@@ -10,139 +10,161 @@ import utest._
1010
1111object 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 }
0 commit comments