Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests to show broken behavior when a symlink is named lib #372

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.netflix.gradle.plugins.utils

import org.gradle.api.file.FileCopyDetails

import java.nio.file.Files
import java.nio.file.LinkOption
import java.nio.file.Path

final class GradleUtils {
Expand Down Expand Up @@ -41,6 +43,13 @@ final class GradleUtils {
File sourceRoot = new File("/$sourceBase", sourceRelative)
File targetRoot = new File("/$sourceBase", targetPath.substring(sourceBasePath.length()))
Path relativeTarget = sourceRoot.isDirectory() ? sourceRoot.toPath().relativize(targetRoot.toPath()) : sourceRoot.parentFile.toPath().relativize(targetRoot.toPath())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interestingly enough, if I just take the fork out of this line and leave the following all tests pass

Path relativeTarget = sourceRoot.parentFile.toPath().relativize(targetRoot.toPath())

It still doesn't answer why the link is being evaluated differently, but I don't ever see a use case for the first option here anyways. I'm probably missing something, but whenever this is successful, it evaluates false. Again, I believe that is correct since it is a link and isDirectory() should return false for that.

Even if we are at a "directory symlink", we would still want the relative path to the target from the parent I believe. Otherwise the path you get is going up out of the "link directory" and back down to the target. Like ../path/to/target. Then the link is broken.

println sourceBase
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just put these in here to dump some output in the test output

println sourceRelative
println Files.isSymbolicLink(sourceRoot.toPath())
println Files.isDirectory(sourceRoot.toPath())
println Files.isDirectory(sourceRoot.toPath(), LinkOption.NOFOLLOW_LINKS)
println sourceRoot.isDirectory()
println relativeTarget
return new Tuple2(sourceRoot.path, relativeTarget.toString())
} else {
return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class Scanner {
header = new CpioHeader()
total = header.read(wrapper, total)
final int fileSize = header.getFileSize()
boolean includingContents = includeContents&&header.type==8
boolean includingContents = includeContents&&(header.type==8||header.type==10)
if (!header.isLast()) {
ByteBuffer descriptor = includingContents?Util.fill(wrapper, fileSize):null
files += new ScannerFile(header, descriptor)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.netflix.gradle.plugins.utils

import static org.redline_rpm.payload.CpioHeader.SYMLINK

import com.netflix.gradle.plugins.rpm.Scanner
import nebula.test.IntegrationSpec
import nebula.test.functional.ExecutionResult

import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths

class GradleUtilsIntegrationTest extends IntegrationSpec {

def 'verifySymlinkDirNested'() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

builds a structure like so :

source
├── sourceDir
│   └── subdir
│       └── text.txt
└── notlib -> sourceDir/subdir

given:
File rootSourceFile = createFile('source/sourceDir/subdir/text.txt')
Path link = Paths.get(projectDir.absolutePath, 'source','notlib')
Files.createSymbolicLink(link, rootSourceFile.parentFile.toPath())

buildFile << """
apply plugin: 'nebula.rpm'

task buildRpm(type: Rpm) {
packageName 'test'
from('source') {
into('/usr/local/')
}
}
"""

when:
ExecutionResult result = runTasksSuccessfully('buildRpm')
println result.standardOutput

then:
File archive = new File(projectDir, 'build/distributions/test-0.noarch.rpm')
archive.exists()
Scanner.ScannerResult scan = Scanner.scan(archive)
Scanner.ScannerFile linkFile = scan.files.find { it.name == './usr/local/notlib'}
linkFile.type == SYMLINK
linkFile.asString() == 'sourceDir/subdir'
}

def 'verifySymlinkDirNestedWithLib'() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

builds a structure like so :

source
├── sourceDir
│   └── subdir
│       └── text.txt
└── lib -> sourceDir/subdir

given:
File rootSourceFile = createFile('source/sourceDir/subdir/text.txt')
Path link = Paths.get(projectDir.absolutePath, 'source','lib')
Files.createSymbolicLink(link, rootSourceFile.parentFile.toPath())

buildFile << """
apply plugin: 'nebula.rpm'

task buildRpm(type: Rpm) {
packageName 'test'
from('source') {
into('/usr/local/')
}
}
"""

when:
ExecutionResult result = runTasksSuccessfully('buildRpm')
println result.standardOutput

then:
File archive = new File(projectDir, 'build/distributions/test-0.noarch.rpm')
archive.exists()
Scanner.ScannerResult scan = Scanner.scan(archive)
Scanner.ScannerFile linkFile = scan.files.find { it.name == './usr/local/lib'}
linkFile.type == SYMLINK
linkFile.asString() == 'sourceDir/subdir'
}

}