Skip to content

Commit

Permalink
Make use of concise literal paths everywhere (#302)
Browse files Browse the repository at this point in the history
Both test suite and docs. We leave those in `PathTests` unchanged, since
that file intentionally contains a mix of different concise and verbose
paths for testing purposes, and we also leave a handful of examples of
verbose paths in the docs to demonstrate it's possible. Otherwise
everywhere uses the concise path syntax
  • Loading branch information
lihaoyi authored Sep 11, 2024
1 parent 69cc8a3 commit e3dd379
Show file tree
Hide file tree
Showing 12 changed files with 424 additions and 409 deletions.
321 changes: 168 additions & 153 deletions Readme.adoc

Large diffs are not rendered by default.

78 changes: 39 additions & 39 deletions os/test/src-jvm/ExampleTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ object ExampleTests extends TestSuite {
test("splash") - TestUtil.prep { wd =>
if (Unix()) {
// Make sure working directory exists and is empty
val wd = os.pwd / "out" / "splash"
val wd = os.pwd / "out/splash"
os.remove.all(wd)
os.makeDir.all(wd)

Expand Down Expand Up @@ -95,8 +95,8 @@ object ExampleTests extends TestSuite {

// ignore multiline (second file) because its size varies
largestThree.filterNot(_._2.last == "Multi Line.txt") ==> Seq(
(711, wd / "misc" / "binary.png"),
(22, wd / "folder1" / "one.txt")
(711, wd / "misc/binary.png"),
(22, wd / "folder1/one.txt")
)
}

Expand All @@ -115,9 +115,9 @@ object ExampleTests extends TestSuite {
}
test("comparison") {

os.remove.all(os.pwd / "out" / "scratch" / "folder" / "thing" / "file")
os.remove.all(os.pwd / "out/scratch/folder/thing/file")
os.write(
os.pwd / "out" / "scratch" / "folder" / "thing" / "file",
os.pwd / "out/scratch/folder/thing/file",
"Hello!",
createFolders = true
)
Expand All @@ -135,16 +135,16 @@ object ExampleTests extends TestSuite {
}
removeAll("out/scratch/folder/thing")

assert(os.list(os.pwd / "out" / "scratch" / "folder").toSeq == Nil)
assert(os.list(os.pwd / "out/scratch/folder").toSeq == Nil)

os.write(
os.pwd / "out" / "scratch" / "folder" / "thing" / "file",
os.pwd / "out/scratch/folder/thing/file",
"Hello!",
createFolders = true
)

os.remove.all(os.pwd / "out" / "scratch" / "folder" / "thing")
assert(os.list(os.pwd / "out" / "scratch" / "folder").toSeq == Nil)
os.remove.all(os.pwd / "out/scratch/folder/thing")
assert(os.list(os.pwd / "out/scratch/folder").toSeq == Nil)
}

test("constructingPaths") {
Expand All @@ -155,13 +155,13 @@ object ExampleTests extends TestSuite {
val wd = os.pwd

// A path nested inside `wd`
wd / "folder" / "file"
wd / "folder/file"

// A path starting from the root
os.root / "folder" / "file"
os.root / "folder/file"

// A path with spaces or other special characters
wd / "My Folder" / "My File.txt"
wd / "My Folder/My File.txt"

// Up one level from the wd
wd / os.up
Expand All @@ -171,17 +171,17 @@ object ExampleTests extends TestSuite {
}
test("newPath") {

val target = os.pwd / "out" / "scratch"
val target = os.pwd / "out/scratch"
}
test("relPaths") {

// The path "folder/file"
val rel1 = os.rel / "folder" / "file"
val rel2 = os.rel / "folder" / "file"
val rel1 = os.rel / "folder/file"
val rel2 = os.rel / "folder/file"

// The relative difference between two paths
val target = os.pwd / "out" / "scratch" / "file"
assert((target relativeTo os.pwd) == os.rel / "out" / "scratch" / "file")
val target = os.pwd / "out/scratch/file"
assert((target relativeTo os.pwd) == os.rel / "out/scratch/file")

// `up`s get resolved automatically
val minus = os.pwd relativeTo target
Expand All @@ -195,66 +195,66 @@ object ExampleTests extends TestSuite {
test("subPaths") {

// The path "folder/file"
val sub1 = os.sub / "folder" / "file"
val sub2 = os.sub / "folder" / "file"
val sub1 = os.sub / "folder/file"
val sub2 = os.sub / "folder/file"

// The relative difference between two paths
val target = os.pwd / "out" / "scratch" / "file"
assert((target subRelativeTo os.pwd) == os.sub / "out" / "scratch" / "file")
val target = os.pwd / "out/scratch/file"
assert((target subRelativeTo os.pwd) == os.sub / "out/scratch/file")

// Converting os.RelPath to os.SubPath
val rel3 = os.rel / "folder" / "file"
val rel3 = os.rel / "folder/file"
val sub3 = rel3.asSubPath

// `up`s are not allowed in sub paths
intercept[Exception](os.pwd subRelativeTo target)
}
test("relSubPathEquality") {
assert(
(os.sub / "hello" / "world") == (os.rel / "hello" / "world"),
(os.sub / "hello/world") == (os.rel / "hello/world"),
os.sub == os.rel
)
}
test("relPathCombine") {
val target = os.pwd / "out" / "scratch" / "file"
val target = os.pwd / "out/scratch/file"
val rel = target relativeTo os.pwd
val newBase = os.root / "code" / "server"
assert(newBase / rel == os.root / "code" / "server" / "out" / "scratch" / "file")
val newBase = os.root / "code/server"
assert(newBase / rel == os.root / "code/server/out/scratch/file")
}
test("subPathCombine") {
val target = os.pwd / "out" / "scratch" / "file"
val target = os.pwd / "out/scratch/file"
val sub = target subRelativeTo os.pwd
val newBase = os.root / "code" / "server"
val newBase = os.root / "code/server"
assert(
newBase / sub == os.root / "code" / "server" / "out" / "scratch" / "file",
sub / sub == os.sub / "out" / "scratch" / "file" / "out" / "scratch" / "file"
newBase / sub == os.root / "code/server/out/scratch/file",
sub / sub == os.sub / "out/scratch/file/out/scratch/file"
)
}
test("pathUp") {
val target = os.root / "out" / "scratch" / "file"
assert(target / os.up == os.root / "out" / "scratch")
val target = os.root / "out/scratch/file"
assert(target / os.up == os.root / "out/scratch")
}
test("relPathUp") {
val target = os.rel / "out" / "scratch" / "file"
assert(target / os.up == os.rel / "out" / "scratch")
val target = os.rel / "out/scratch/file"
assert(target / os.up == os.rel / "out/scratch")
}
test("relPathUp") {
val target = os.sub / "out" / "scratch" / "file"
assert(target / os.up == os.sub / "out" / "scratch")
val target = os.sub / "out/scratch/file"
assert(target / os.up == os.sub / "out/scratch")
}
test("canonical") {
if (Unix()) {

assert((os.root / "folder" / "file" / os.up).toString == "/folder")
assert((os.root / "folder/file" / os.up).toString == "/folder")
// not "/folder/file/.."

assert((os.rel / "folder" / "file" / os.up).toString == "folder")
assert((os.rel / "folder/file" / os.up).toString == "folder")
// not "folder/file/.."
}
}
test("findWc") {

val wd = os.pwd / "os" / "test" / "resources" / "test"
val wd = os.pwd / "os/test/resources/test"

// find . -name '*.txt' | xargs wc -l
val lines = os.walk(wd)
Expand Down
28 changes: 14 additions & 14 deletions os/test/src-jvm/OpTestsJvmOnly.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,48 +10,48 @@ import java.nio.charset.Charset
object OpTestsJvmOnly extends TestSuite {

val tests = Tests {
val res = os.pwd / "os" / "test" / "resources" / "test"
val testFolder = os.pwd / "out" / "scratch" / "test"
val res = os.pwd / "os/test/resources/test"
val testFolder = os.pwd / "out/scratch/test"
test("lsRecPermissions") {
if (Unix()) {
assert(os.walk(os.root / "var" / "run").nonEmpty)
assert(os.walk(os.root / "var/run").nonEmpty)
}
}
test("readResource") {
test("positive") {
test("absolute") {
val contents = os.read(os.resource / "test" / "os" / "folder" / "file.txt")
val contents = os.read(os.resource / "test/os/folder/file.txt")
assert(contents.contains("file contents lols"))

val cl = getClass.getClassLoader
val contents2 = os.read(os.resource(cl) / "test" / "os" / "folder" / "file.txt")
val contents2 = os.read(os.resource(cl) / "test/os/folder/file.txt")
assert(contents2.contains("file contents lols"))
}

test("relative") {
val cls = classOf[_root_.test.os.Testing]
val contents = os.read(os.resource(cls) / "folder" / "file.txt")
val contents = os.read(os.resource(cls) / "folder/file.txt")
assert(contents.contains("file contents lols"))

val contents2 = os.read(os.resource(getClass) / "folder" / "file.txt")
val contents2 = os.read(os.resource(getClass) / "folder/file.txt")
assert(contents2.contains("file contents lols"))
}
}
test("negative") {
test - intercept[os.ResourceNotFoundException] {
os.read(os.resource / "folder" / "file.txt")
os.read(os.resource / "folder/file.txt")
}

test - intercept[os.ResourceNotFoundException] {
os.read(
os.resource(classOf[_root_.test.os.Testing]) / "test" / "os" / "folder" / "file.txt"
os.resource(classOf[_root_.test.os.Testing]) / "test/os/folder/file.txt"
)
}
test - intercept[os.ResourceNotFoundException] {
os.read(os.resource(getClass) / "test" / "os" / "folder" / "file.txt")
os.read(os.resource(getClass) / "test/os/folder/file.txt")
}
test - intercept[os.ResourceNotFoundException] {
os.read(os.resource(getClass.getClassLoader) / "folder" / "file.txt")
os.read(os.resource(getClass.getClassLoader) / "folder/file.txt")
}
}
}
Expand All @@ -74,16 +74,16 @@ object OpTestsJvmOnly extends TestSuite {
// Not sure why this doesn't work on native
test("redirectSubprocessInheritedOutput") {
if (Unix()) { // relies on bash scripts that don't run on windows
val scriptFolder = os.pwd / "os" / "test" / "resources" / "test"
val scriptFolder = os.pwd / "os/test/resources/test"
val lines = collection.mutable.Buffer.empty[String]
os.Inherit.out.withValue(os.ProcessOutput.Readlines(lines.append(_))) {
// Redirected
os.proc(scriptFolder / "misc" / "echo_with_wd", "HELLO\nWorld").call(
os.proc(scriptFolder / "misc/echo_with_wd", "HELLO\nWorld").call(
cwd = os.root / "usr",
stdout = os.Inherit
)
// Not Redirected
os.proc(scriptFolder / "misc" / "echo_with_wd", "hello\nWORLD").call(
os.proc(scriptFolder / "misc/echo_with_wd", "hello\nWORLD").call(
cwd = os.root / "usr",
stdout = os.InheritRaw
)
Expand Down
Loading

0 comments on commit e3dd379

Please sign in to comment.