Skip to content

Commit 878aaa1

Browse files
authored
Cleanup of Tests (#162)
Pull request: #162
1 parent 5470f04 commit 878aaa1

File tree

4 files changed

+154
-141
lines changed

4 files changed

+154
-141
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package test.os
2+
3+
object ExampleResourcess {
4+
5+
object RemoteReadme {
6+
val url =
7+
"https://raw.githubusercontent.com/lihaoyi/os-lib/d6695db4e484afac2c0adf67016cd5d3df6b92ae/readme.md"
8+
val shortUrl = "https://git.io/fpfTs"
9+
// curl -L https://git.io/fpfTs | gzip -n | shasum -a 256
10+
val gzip6ShaSum256 = "acc142175fa520a1cb2be5b97cbbe9bea092e8bba3fe2e95afa645615908229e"
11+
val size = 53814
12+
}
13+
}

os/test/src-jvm/ExampleTests.scala

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ object ExampleTests extends TestSuite{
2020

2121
val invoked = os.proc("cat", wd/"file.txt", wd/"copied.txt").call(cwd = wd)
2222
invoked.out.trim() ==> "hellohello"
23-
24-
val curl = os.proc("curl", "-L" , "https://git.io/fpfTs").spawn(stderr = os.Inherit)
25-
val gzip = os.proc("gzip", "-n").spawn(stdin = curl.stdout)
26-
val sha = os.proc("shasum", "-a", "256").spawn(stdin = gzip.stdout)
27-
sha.stdout.trim() ==> "acc142175fa520a1cb2be5b97cbbe9bea092e8bba3fe2e95afa645615908229e -"
2823
}}
2924

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

63-
os.size(temp) ==> 53814
58+
os.size(temp) ==> ExampleResourcess.RemoteReadme.size
6459

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

6964
os.write.over(temp2, proc.stdout)
70-
os.size(temp2) ==> 53814
65+
os.size(temp2) ==> ExampleResourcess.RemoteReadme.size
66+
67+
assert(os.size(temp) == os.size(temp2))
7168
}}
7269

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

259-
test("Source code line length does not exceed 100"){
260-
261-
// Ensure that we don't have any Scala files in the current working directory
262-
// which have lines more than 100 characters long, excluding generated sources
263-
// in `src_managed` folders.
264-
265-
def longLines(p: os.Path) =
266-
(p, os.read.lines(p).zipWithIndex.filter(_._1.length > 100).map(_._2))
267-
268-
val filesWithTooLongLines =
269-
os.proc("git", "ls-files").call(cwd = os.pwd).out.lines()
270-
.map(os.Path(_, os.pwd))
271-
.filter(_.ext == "scala")
272-
.map(longLines)
273-
.filter(_._2.length > 0)
274-
.filter(!_._1.segments.contains("src_managed"))
275-
276-
Predef.assert(filesWithTooLongLines.length == 0, filesWithTooLongLines)
277-
}
278256
test("rename"){
279257
// val d1/"omg"/x1 = wd
280258
// val d2/"omg"/x2 = wd

os/test/src-jvm/SpawningSubprocessesTests.scala

Lines changed: 134 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -10,139 +10,161 @@ import utest._
1010

1111
object SpawningSubprocessesTests extends TestSuite {
1212

13-
def tests = Tests{
14-
test("proc"){
15-
test("call"){
16-
test - prep { wd => if(Unix()){
17-
val res = os.proc("ls", wd/"folder2").call()
18-
19-
res.exitCode ==> 0
20-
21-
res.out.text() ==>
22-
"""nestedA
23-
|nestedB
24-
|""".stripMargin
25-
26-
res.out.trim() ==>
27-
"""nestedA
28-
|nestedB""".stripMargin
29-
30-
res.out.lines() ==> Seq(
31-
"nestedA",
32-
"nestedB"
33-
)
34-
35-
res.out.bytes
13+
def tests = Tests {
14+
test("proc") {
15+
test("call") {
16+
test - prep { wd =>
17+
if (Unix()) {
18+
val res = os.proc("ls", wd / "folder2").call()
19+
20+
res.exitCode ==> 0
21+
22+
res.out.text() ==>
23+
"""nestedA
24+
|nestedB
25+
|""".stripMargin
26+
27+
res.out.trim() ==>
28+
"""nestedA
29+
|nestedB""".stripMargin
30+
31+
res.out.lines() ==> Seq(
32+
"nestedA",
33+
"nestedB"
34+
)
3635

36+
res.out.bytes
3737

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

42-
assert(thrown.result.exitCode != 0)
42+
assert(thrown.result.exitCode != 0)
4343

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

46-
assert(fail.exitCode != 0)
46+
assert(fail.exitCode != 0)
4747

48-
fail.out.text() ==> ""
48+
fail.out.text() ==> ""
4949

50-
assert(fail.err.text().contains("No such file or directory"))
50+
assert(fail.err.text().contains("No such file or directory"))
5151

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

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

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

61-
if (false){
62-
os.proc("vim").call(stdin = os.Inherit, stdout = os.Inherit, stderr = os.Inherit)
61+
if (false) {
62+
os.proc("vim").call(stdin = os.Inherit, stdout = os.Inherit, stderr = os.Inherit)
63+
}
6364
}
64-
}}
65-
test - prep{wd =>if(Unix()){
66-
val ex = intercept[os.SubprocessException]{
67-
os.proc("bash", "-c", "echo 123; sleep 10; echo 456")
68-
.call(timeout = 2000)
65+
}
66+
test - prep { wd =>
67+
if (Unix()) {
68+
val ex = intercept[os.SubprocessException] {
69+
os.proc("bash", "-c", "echo 123; sleep 10; echo 456")
70+
.call(timeout = 2000)
71+
}
72+
73+
ex.result.out.trim() ==> "123"
6974
}
70-
71-
ex.result.out.trim()==> "123"
72-
}}
75+
}
7376
}
74-
test("stream"){
75-
test - prep { wd => if(Unix()){
76-
var lineCount = 1
77-
os.proc("find", ".").call(
78-
cwd = wd,
79-
stdout = os.ProcessOutput(
80-
(buf, len) => lineCount += buf.slice(0, len).count(_ == '\n')
77+
test("stream") {
78+
test - prep { wd =>
79+
if (Unix()) {
80+
var lineCount = 1
81+
os.proc("find", ".").call(
82+
cwd = wd,
83+
stdout =
84+
os.ProcessOutput((buf, len) => lineCount += buf.slice(0, len).count(_ == '\n'))
8185
)
82-
)
83-
lineCount ==> 22
84-
}}
85-
test - prep { wd => if(Unix()){
86-
var lineCount = 1
87-
os.proc("find", ".").call(
88-
cwd = wd,
89-
stdout = os.ProcessOutput.Readlines(
90-
line => lineCount += 1
86+
lineCount ==> 22
87+
}
88+
}
89+
test - prep { wd =>
90+
if (Unix()) {
91+
var lineCount = 1
92+
os.proc("find", ".").call(
93+
cwd = wd,
94+
stdout = os.ProcessOutput.Readlines(line => lineCount += 1)
9195
)
92-
)
93-
lineCount ==> 22
94-
}}
96+
lineCount ==> 22
97+
}
98+
}
9599
}
96100

97-
test("spawn"){
98-
test - prep { wd => if(TestUtil.isInstalled("python") && Unix()) {
99-
// Start a long-lived python process which you can communicate with
100-
val sub = os.proc("python", "-u", "-c",
101-
if (TestUtil.isPython3()) "while True: print(eval(input()))"
102-
else "while True: print(eval(raw_input()))"
103-
)
104-
.spawn(cwd = wd)
105-
106-
// Sending some text to the subprocess
107-
sub.stdin.write("1 + 2")
108-
sub.stdin.writeLine("+ 4")
109-
sub.stdin.flush()
110-
sub.stdout.readLine() ==> "7"
111-
112-
sub.stdin.write("'1' + '2'")
113-
sub.stdin.writeLine("+ '4'")
114-
sub.stdin.flush()
115-
sub.stdout.readLine() ==> "124"
116-
117-
// Sending some bytes to the subprocess
118-
sub.stdin.write("1 * 2".getBytes)
119-
sub.stdin.write("* 4\n".getBytes)
120-
sub.stdin.flush()
121-
sub.stdout.read() ==> '8'.toByte
122-
123-
sub.destroy()
124-
101+
test("spawn python") {
102+
test - prep { wd =>
103+
if (TestUtil.isInstalled("python") && Unix()) {
104+
// Start a long-lived python process which you can communicate with
105+
val sub = os.proc(
106+
"python",
107+
"-u",
108+
"-c",
109+
if (TestUtil.isPython3()) "while True: print(eval(input()))"
110+
else "while True: print(eval(raw_input()))"
111+
)
112+
.spawn(cwd = wd)
113+
114+
// Sending some text to the subprocess
115+
sub.stdin.write("1 + 2")
116+
sub.stdin.writeLine("+ 4")
117+
sub.stdin.flush()
118+
sub.stdout.readLine() ==> "7"
119+
120+
sub.stdin.write("'1' + '2'")
121+
sub.stdin.writeLine("+ '4'")
122+
sub.stdin.flush()
123+
sub.stdout.readLine() ==> "124"
124+
125+
// Sending some bytes to the subprocess
126+
sub.stdin.write("1 * 2".getBytes)
127+
sub.stdin.write("* 4\n".getBytes)
128+
sub.stdin.flush()
129+
sub.stdout.read() ==> '8'.toByte
130+
131+
sub.destroy()
132+
}
133+
}
134+
}
135+
test("spawn curl") {
136+
if (
137+
Unix() && // shasum seems to not accept stdin on Windows
138+
TestUtil.isInstalled("curl") &&
139+
TestUtil.isInstalled("gzip") &&
140+
TestUtil.isInstalled("shasum")
141+
) {
125142
// You can chain multiple subprocess' stdin/stdout together
126-
val curl = os.proc("curl", "-L" , "https://git.io/fpfTs").spawn(stderr = os.Inherit)
127-
val gzip = os.proc("gzip", "-n").spawn(stdin = curl.stdout)
143+
val curl =
144+
os.proc("curl", "-L", ExampleResourcess.RemoteReadme.url).spawn(stderr = os.Inherit)
145+
val gzip = os.proc("gzip", "-n", "-6").spawn(stdin = curl.stdout)
128146
val sha = os.proc("shasum", "-a", "256").spawn(stdin = gzip.stdout)
129-
sha.stdout.trim()==> "acc142175fa520a1cb2be5b97cbbe9bea092e8bba3fe2e95afa645615908229e -"
130-
}}
147+
sha.stdout.trim() ==> s"${ExampleResourcess.RemoteReadme.gzip6ShaSum256} -"
148+
}
131149
}
132-
test("spawn callback"){
133-
test - prep { wd => if(TestUtil.isInstalled("python") && Unix()) {
134-
val output: mutable.Buffer[String] = mutable.Buffer()
135-
val sub = os.proc("echo", "output")
136-
.spawn(stdout = ProcessOutput((bytes, count) => output += new String(bytes, 0, count)))
137-
val finished = sub.join(5000)
138-
sub.wrapped.getOutputStream().flush()
139-
assert(finished)
140-
assert(sub.exitCode() == 0)
141-
val expectedOutput = "output\n"
142-
val actualOutput = output.mkString("")
143-
assert(actualOutput == expectedOutput)
144-
sub.destroy()
145-
}}
150+
test("spawn callback") {
151+
test - prep { wd =>
152+
if (TestUtil.isInstalled("echo") && Unix()) {
153+
val output: mutable.Buffer[String] = mutable.Buffer()
154+
val sub = os.proc("echo", "output")
155+
.spawn(stdout =
156+
ProcessOutput((bytes, count) => output += new String(bytes, 0, count))
157+
)
158+
val finished = sub.join(5000)
159+
sub.wrapped.getOutputStream().flush()
160+
assert(finished)
161+
assert(sub.exitCode() == 0)
162+
val expectedOutput = "output\n"
163+
val actualOutput = output.mkString("")
164+
assert(actualOutput == expectedOutput)
165+
sub.destroy()
166+
}
167+
}
146168
}
147169
}
148170
}

os/test/src/unix.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package test.os
44
* Created by haoyi on 2/17/16.
55
*/
66
object Unix {
7-
def apply() = java.nio.file.Paths.get("").toAbsolutePath.getRoot.toString == "/"
7+
def apply(): Boolean = java.nio.file.Paths.get("").toAbsolutePath.getRoot.toString == "/"
88
}
99

1010
/**

0 commit comments

Comments
 (0)