Skip to content

Commit

Permalink
Cleanup of Tests (#162)
Browse files Browse the repository at this point in the history
Pull request: #162
  • Loading branch information
lefou authored Apr 4, 2023
1 parent 5470f04 commit 878aaa1
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 141 deletions.
13 changes: 13 additions & 0 deletions os/test/src-jvm/ExampleResourcess.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package test.os

object ExampleResourcess {

object RemoteReadme {
val url =
"https://raw.githubusercontent.com/lihaoyi/os-lib/d6695db4e484afac2c0adf67016cd5d3df6b92ae/readme.md"
val shortUrl = "https://git.io/fpfTs"
// curl -L https://git.io/fpfTs | gzip -n | shasum -a 256
val gzip6ShaSum256 = "acc142175fa520a1cb2be5b97cbbe9bea092e8bba3fe2e95afa645615908229e"
val size = 53814
}
}
34 changes: 6 additions & 28 deletions os/test/src-jvm/ExampleTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ object ExampleTests extends TestSuite{

val invoked = os.proc("cat", wd/"file.txt", wd/"copied.txt").call(cwd = wd)
invoked.out.trim() ==> "hellohello"

val curl = os.proc("curl", "-L" , "https://git.io/fpfTs").spawn(stderr = os.Inherit)
val gzip = os.proc("gzip", "-n").spawn(stdin = curl.stdout)
val sha = os.proc("shasum", "-a", "256").spawn(stdin = gzip.stdout)
sha.stdout.trim() ==> "acc142175fa520a1cb2be5b97cbbe9bea092e8bba3fe2e95afa645615908229e -"
}}

test("concatTxt") - TestUtil.prep{wd =>
Expand Down Expand Up @@ -57,17 +52,19 @@ object ExampleTests extends TestSuite{
test("curlToTempFile") - TestUtil.prep{wd => if (Unix()){
// Curl to temporary file
val temp = os.temp()
os.proc("curl", "-L" , "https://git.io/fpfTs")
os.proc("curl", "-L" , ExampleResourcess.RemoteReadme.url)
.call(stdout = temp)

os.size(temp) ==> 53814
os.size(temp) ==> ExampleResourcess.RemoteReadme.size

// Curl to temporary file
val temp2 = os.temp()
val proc = os.proc("curl", "-L" , "https://git.io/fpfTs").spawn()
val proc = os.proc("curl", "-L" , ExampleResourcess.RemoteReadme.url).spawn()

os.write.over(temp2, proc.stdout)
os.size(temp2) ==> 53814
os.size(temp2) ==> ExampleResourcess.RemoteReadme.size

assert(os.size(temp) == os.size(temp2))
}}

test("lineCount") - TestUtil.prep{wd =>
Expand Down Expand Up @@ -256,25 +253,6 @@ object ExampleTests extends TestSuite{
assert(lines == 9)
}

test("Source code line length does not exceed 100"){

// Ensure that we don't have any Scala files in the current working directory
// which have lines more than 100 characters long, excluding generated sources
// in `src_managed` folders.

def longLines(p: os.Path) =
(p, os.read.lines(p).zipWithIndex.filter(_._1.length > 100).map(_._2))

val filesWithTooLongLines =
os.proc("git", "ls-files").call(cwd = os.pwd).out.lines()
.map(os.Path(_, os.pwd))
.filter(_.ext == "scala")
.map(longLines)
.filter(_._2.length > 0)
.filter(!_._1.segments.contains("src_managed"))

Predef.assert(filesWithTooLongLines.length == 0, filesWithTooLongLines)
}
test("rename"){
// val d1/"omg"/x1 = wd
// val d2/"omg"/x2 = wd
Expand Down
246 changes: 134 additions & 112 deletions os/test/src-jvm/SpawningSubprocessesTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,139 +10,161 @@ import utest._

object SpawningSubprocessesTests extends TestSuite {

def tests = Tests{
test("proc"){
test("call"){
test - prep { wd => if(Unix()){
val res = os.proc("ls", wd/"folder2").call()

res.exitCode ==> 0

res.out.text() ==>
"""nestedA
|nestedB
|""".stripMargin

res.out.trim() ==>
"""nestedA
|nestedB""".stripMargin

res.out.lines() ==> Seq(
"nestedA",
"nestedB"
)

res.out.bytes
def tests = Tests {
test("proc") {
test("call") {
test - prep { wd =>
if (Unix()) {
val res = os.proc("ls", wd / "folder2").call()

res.exitCode ==> 0

res.out.text() ==>
"""nestedA
|nestedB
|""".stripMargin

res.out.trim() ==>
"""nestedA
|nestedB""".stripMargin

res.out.lines() ==> Seq(
"nestedA",
"nestedB"
)

res.out.bytes

val thrown = intercept[os.SubprocessException]{
os.proc("ls", "doesnt-exist").call(cwd = wd)
}
val thrown = intercept[os.SubprocessException] {
os.proc("ls", "doesnt-exist").call(cwd = wd)
}

assert(thrown.result.exitCode != 0)
assert(thrown.result.exitCode != 0)

val fail = os.proc("ls", "doesnt-exist").call(cwd = wd, check = false, stderr = os.Pipe)
val fail = os.proc("ls", "doesnt-exist").call(cwd = wd, check = false, stderr = os.Pipe)

assert(fail.exitCode != 0)
assert(fail.exitCode != 0)

fail.out.text() ==> ""
fail.out.text() ==> ""

assert(fail.err.text().contains("No such file or directory"))
assert(fail.err.text().contains("No such file or directory"))

// You can pass in data to a subprocess' stdin
val hash = os.proc("shasum", "-a", "256").call(stdin = "Hello World")
hash.out.trim() ==> "a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e -"
// You can pass in data to a subprocess' stdin
val hash = os.proc("shasum", "-a", "256").call(stdin = "Hello World")
hash.out.trim() ==> "a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e -"

// Taking input from a file and directing output to another file
os.proc("base64").call(stdin = wd / "File.txt", stdout = wd / "File.txt.b64")
// Taking input from a file and directing output to another file
os.proc("base64").call(stdin = wd / "File.txt", stdout = wd / "File.txt.b64")

os.read(wd / "File.txt.b64") ==> "SSBhbSBjb3c=\n"
os.read(wd / "File.txt.b64") ==> "SSBhbSBjb3c=\n"

if (false){
os.proc("vim").call(stdin = os.Inherit, stdout = os.Inherit, stderr = os.Inherit)
if (false) {
os.proc("vim").call(stdin = os.Inherit, stdout = os.Inherit, stderr = os.Inherit)
}
}
}}
test - prep{wd =>if(Unix()){
val ex = intercept[os.SubprocessException]{
os.proc("bash", "-c", "echo 123; sleep 10; echo 456")
.call(timeout = 2000)
}
test - prep { wd =>
if (Unix()) {
val ex = intercept[os.SubprocessException] {
os.proc("bash", "-c", "echo 123; sleep 10; echo 456")
.call(timeout = 2000)
}

ex.result.out.trim() ==> "123"
}

ex.result.out.trim()==> "123"
}}
}
}
test("stream"){
test - prep { wd => if(Unix()){
var lineCount = 1
os.proc("find", ".").call(
cwd = wd,
stdout = os.ProcessOutput(
(buf, len) => lineCount += buf.slice(0, len).count(_ == '\n')
test("stream") {
test - prep { wd =>
if (Unix()) {
var lineCount = 1
os.proc("find", ".").call(
cwd = wd,
stdout =
os.ProcessOutput((buf, len) => lineCount += buf.slice(0, len).count(_ == '\n'))
)
)
lineCount ==> 22
}}
test - prep { wd => if(Unix()){
var lineCount = 1
os.proc("find", ".").call(
cwd = wd,
stdout = os.ProcessOutput.Readlines(
line => lineCount += 1
lineCount ==> 22
}
}
test - prep { wd =>
if (Unix()) {
var lineCount = 1
os.proc("find", ".").call(
cwd = wd,
stdout = os.ProcessOutput.Readlines(line => lineCount += 1)
)
)
lineCount ==> 22
}}
lineCount ==> 22
}
}
}

test("spawn"){
test - prep { wd => if(TestUtil.isInstalled("python") && Unix()) {
// Start a long-lived python process which you can communicate with
val sub = os.proc("python", "-u", "-c",
if (TestUtil.isPython3()) "while True: print(eval(input()))"
else "while True: print(eval(raw_input()))"
)
.spawn(cwd = wd)

// Sending some text to the subprocess
sub.stdin.write("1 + 2")
sub.stdin.writeLine("+ 4")
sub.stdin.flush()
sub.stdout.readLine() ==> "7"

sub.stdin.write("'1' + '2'")
sub.stdin.writeLine("+ '4'")
sub.stdin.flush()
sub.stdout.readLine() ==> "124"

// Sending some bytes to the subprocess
sub.stdin.write("1 * 2".getBytes)
sub.stdin.write("* 4\n".getBytes)
sub.stdin.flush()
sub.stdout.read() ==> '8'.toByte

sub.destroy()

test("spawn python") {
test - prep { wd =>
if (TestUtil.isInstalled("python") && Unix()) {
// Start a long-lived python process which you can communicate with
val sub = os.proc(
"python",
"-u",
"-c",
if (TestUtil.isPython3()) "while True: print(eval(input()))"
else "while True: print(eval(raw_input()))"
)
.spawn(cwd = wd)

// Sending some text to the subprocess
sub.stdin.write("1 + 2")
sub.stdin.writeLine("+ 4")
sub.stdin.flush()
sub.stdout.readLine() ==> "7"

sub.stdin.write("'1' + '2'")
sub.stdin.writeLine("+ '4'")
sub.stdin.flush()
sub.stdout.readLine() ==> "124"

// Sending some bytes to the subprocess
sub.stdin.write("1 * 2".getBytes)
sub.stdin.write("* 4\n".getBytes)
sub.stdin.flush()
sub.stdout.read() ==> '8'.toByte

sub.destroy()
}
}
}
test("spawn curl") {
if (
Unix() && // shasum seems to not accept stdin on Windows
TestUtil.isInstalled("curl") &&
TestUtil.isInstalled("gzip") &&
TestUtil.isInstalled("shasum")
) {
// You can chain multiple subprocess' stdin/stdout together
val curl = os.proc("curl", "-L" , "https://git.io/fpfTs").spawn(stderr = os.Inherit)
val gzip = os.proc("gzip", "-n").spawn(stdin = curl.stdout)
val curl =
os.proc("curl", "-L", ExampleResourcess.RemoteReadme.url).spawn(stderr = os.Inherit)
val gzip = os.proc("gzip", "-n", "-6").spawn(stdin = curl.stdout)
val sha = os.proc("shasum", "-a", "256").spawn(stdin = gzip.stdout)
sha.stdout.trim()==> "acc142175fa520a1cb2be5b97cbbe9bea092e8bba3fe2e95afa645615908229e -"
}}
sha.stdout.trim() ==> s"${ExampleResourcess.RemoteReadme.gzip6ShaSum256} -"
}
}
test("spawn callback"){
test - prep { wd => if(TestUtil.isInstalled("python") && Unix()) {
val output: mutable.Buffer[String] = mutable.Buffer()
val sub = os.proc("echo", "output")
.spawn(stdout = ProcessOutput((bytes, count) => output += new String(bytes, 0, count)))
val finished = sub.join(5000)
sub.wrapped.getOutputStream().flush()
assert(finished)
assert(sub.exitCode() == 0)
val expectedOutput = "output\n"
val actualOutput = output.mkString("")
assert(actualOutput == expectedOutput)
sub.destroy()
}}
test("spawn callback") {
test - prep { wd =>
if (TestUtil.isInstalled("echo") && Unix()) {
val output: mutable.Buffer[String] = mutable.Buffer()
val sub = os.proc("echo", "output")
.spawn(stdout =
ProcessOutput((bytes, count) => output += new String(bytes, 0, count))
)
val finished = sub.join(5000)
sub.wrapped.getOutputStream().flush()
assert(finished)
assert(sub.exitCode() == 0)
val expectedOutput = "output\n"
val actualOutput = output.mkString("")
assert(actualOutput == expectedOutput)
sub.destroy()
}
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion os/test/src/unix.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package test.os
* Created by haoyi on 2/17/16.
*/
object Unix {
def apply() = java.nio.file.Paths.get("").toAbsolutePath.getRoot.toString == "/"
def apply(): Boolean = java.nio.file.Paths.get("").toAbsolutePath.getRoot.toString == "/"
}

/**
Expand Down

0 comments on commit 878aaa1

Please sign in to comment.