Skip to content
Snippets Groups Projects
Commit 53cc2a4d authored by Peter van 't Hof's avatar Peter van 't Hof
Browse files

Fixed bug in Ln

parent f941cb35
No related branches found
No related tags found
No related merge requests found
......@@ -49,52 +49,32 @@ class Ln(val root: Configurable) extends InProcessFunction with Configurable {
/** return commandline to execute */
lazy val cmd: String = {
lazy val inCanonical: String = {
val inCanonical: String = {
// need to remove "/~" to correctly expand path with tilde
input.getAbsolutePath.replace("/~", "")
}
lazy val outCanonical: String = output.getAbsolutePath.replace("/~", "")
val outCanonical: String = output.getAbsolutePath.replace("/~", "")
lazy val inToks: Array[String] = inCanonical.split(File.separator)
if (relative) {
val inToks: Array[String] = inCanonical.split(File.separator)
lazy val outToks: Array[String] = outCanonical.split(File.separator)
val outToks: Array[String] = outCanonical.split(File.separator)
lazy val commonPrefixLength: Int = {
val maxLength = scala.math.min(inToks.length, outToks.length)
var i: Int = 0
while (i < maxLength && inToks(i) == outToks(i)) i += 1
i
}
val commonPrefixLength: Int = {
val maxLength = scala.math.min(inToks.length, outToks.length)
var i: Int = 0
while (i < maxLength && inToks(i) == outToks(i)) i += 1
i
}
lazy val inUnique: String = {
inToks.slice(commonPrefixLength, inToks.length).mkString(File.separator)
}
val inUnique = inToks.slice(commonPrefixLength, inToks.length)
lazy val outUnique: String = {
outToks.slice(commonPrefixLength, outToks.length).mkString(File.separator)
}
val outUnique = outToks.slice(commonPrefixLength, outToks.length)
lazy val inRelative: String = {
// calculate 'distance' from output directory to input
// which is the number of directory walks required to get to the inUnique directory from outDir
val dist =
// relative path differs depending on which of the input or target is in the 'higher' directory
if (inToks.length > outToks.length)
scala.math.max(0, inUnique.split(File.separator).length - 1)
else
scala.math.max(0, outUnique.split(File.separator).length - 1)
val result =
if (dist == 0 || inToks.length > outToks.length)
inUnique
else
((".." + File.separator) * dist) + inUnique
result
}
val inRelative: String =
((".." + File.separator) * (outUnique.length - 1)) + inUnique.mkString(File.separator)
if (relative) {
// workaround until we have `ln` that works with relative path (i.e. `ln -r`)
"ln -s " + inRelative + " " + outCanonical
} else {
......
......@@ -59,6 +59,42 @@ class LnTest extends TestNGSuite with Matchers {
ln.cmd should ===("ln -s ../another_nested/target.txt /dir/nested/link.txt")
}
@Test(description = "Target is a child of a directory multi level above link, relative set to true")
def testTargetMultiLevelAboveChildRelative1() {
val ln = new Ln(null)
ln.relative = true
ln.input = new File("/dir/another_nested/1/2/3/4/target.txt")
ln.output = new File("/dir/nested/link.txt")
ln.cmd should ===("ln -s ../another_nested/1/2/3/4/target.txt /dir/nested/link.txt")
}
@Test(description = "Target is a child of a directory multi level above link, relative set to true")
def testTargetMultiLevelAboveChildRelative2() {
val ln = new Ln(null)
ln.relative = true
ln.input = new File("/dir/another_nested/1/2/3/4/target.txt")
ln.output = new File("/dir/nested/2/link.txt")
ln.cmd should ===("ln -s ../../another_nested/1/2/3/4/target.txt /dir/nested/2/link.txt")
}
@Test(description = "Source is a child of a directory multi level above link, relative set to true")
def testSourceMultiLevelAboveChildRelative() {
val ln = new Ln(null)
ln.relative = true
ln.output = new File("/dir/another_nested/1/2/3/4/link.txt")
ln.input = new File("/dir/nested/2/output.txt")
ln.cmd should ===("ln -s ../../../../../nested/2/output.txt /dir/another_nested/1/2/3/4/link.txt")
}
@Test(description = "Target is a child of a directory multi level above link, relative set to false")
def testTargetMultiLevelAboveChild() {
val ln = new Ln(null)
ln.relative = false
ln.input = new File("/dir/another_nested/1/2/3/4/target.txt")
ln.output = new File("/dir/nested/link.txt")
ln.cmd should ===("ln -s /dir/another_nested/1/2/3/4/target.txt /dir/nested/link.txt")
}
@Test(description = "Target is one level below link, relative set to true")
def testTargetOneLevelBelowRelative() {
val ln = new Ln(null)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment