-
Notifications
You must be signed in to change notification settings - Fork 129
/
RunTestDefinitions.scala
2523 lines (2366 loc) · 81.2 KB
/
RunTestDefinitions.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package scala.cli.integration
import com.eed3si9n.expecty.Expecty.expect
import java.io.{ByteArrayOutputStream, File}
import java.nio.charset.Charset
import scala.cli.integration.util.DockerServer
import scala.io.Codec
import scala.jdk.CollectionConverters._
import scala.util.Properties
abstract class RunTestDefinitions(val scalaVersionOpt: Option[String])
extends ScalaCliSuite with TestScalaVersionArgs {
protected lazy val extraOptions: Seq[String] = scalaVersionArgs ++ TestUtil.extraOptions
protected val ciOpt: Seq[String] =
Option(System.getenv("CI")).map(v => Seq("-e", s"CI=$v")).getOrElse(Nil)
def simpleScriptTest(ignoreErrors: Boolean = false, extraArgs: Seq[String] = Nil): Unit = {
val fileName = "simple.sc"
val message = "Hello"
val inputs = TestInputs(
os.rel / fileName ->
s"""val msg = "$message"
|println(msg)
|""".stripMargin
)
inputs.fromRoot { root =>
val output =
os.proc(TestUtil.cli, extraOptions, extraArgs, fileName).call(cwd = root).out.trim()
if (!ignoreErrors)
expect(output == message)
}
}
// warm-up run that downloads compiler bridges
// The "Downloading compiler-bridge (from bloop?) pollute the output, and would make the first test fail.
lazy val warmupTest: Unit = {
System.err.println("Running RunTests warmup test…")
simpleScriptTest(ignoreErrors = true)
System.err.println("Done running RunTests warmup test.")
}
override def test(name: String)(body: => Any)(implicit loc: munit.Location): Unit =
super.test(name) { warmupTest; body }(loc)
override def test(name: munit.TestOptions)(body: => Any)(implicit loc: munit.Location): Unit =
super.test(name) { warmupTest; body }(loc)
test("simple script") {
simpleScriptTest()
}
test("verbosity") {
simpleScriptTest(extraArgs = Seq("-v"))
}
test("print command") {
val fileName = "simple.sc"
val message = "Hello"
val inputs = TestInputs(
os.rel / fileName ->
s"""val msg = "$message"
|println(msg)
|""".stripMargin
)
inputs.fromRoot { root =>
val output =
os.proc(TestUtil.cli, extraOptions, fileName, "--command").call(cwd = root).out.trim()
val command = output.linesIterator.toVector
val actualOutput = os.proc(command).call(cwd = root).out.trim()
expect(actualOutput == message)
}
}
test("manifest") {
val message = "Hello"
val converters =
if (actualScalaVersion.startsWith("2.12.")) "scala.collection.JavaConverters._"
else "scala.jdk.CollectionConverters._"
val inputs = TestInputs(
os.rel / "Simple.scala" ->
s"""import java.io.File
|import java.util.zip.ZipFile
|import $converters
|
|object Simple {
| private def manifestClassPathCheck(): Unit = {
| val cp = sys.props("java.class.path")
| assert(!cp.contains(File.pathSeparator), s"Expected single entry in class path, got $$cp")
| val zf = new ZipFile(new File(cp))
| val entries = zf.entries.asScala.map(_.getName).toVector
| zf.close()
| assert(entries == Seq("META-INF/MANIFEST.MF"), s"Expected only META-INF/MANIFEST.MF entry, got $$entries")
| }
| def main(args: Array[String]): Unit = {
| manifestClassPathCheck()
| val msg = "$message"
| println(msg)
| }
|}
|""".stripMargin
)
inputs.fromRoot { root =>
val output = os.proc(TestUtil.cli, extraOptions, "--use-manifest", ".")
.call(cwd = root)
.out.trim()
expect(output == message)
}
}
def simpleJsTest(extraArgs: String*): Unit = {
val fileName = "simple.sc"
val message = "Hello"
val inputs = TestInputs(
os.rel / fileName ->
s"""import scala.scalajs.js
|val console = js.Dynamic.global.console
|val msg = "$message"
|console.log(msg)
|""".stripMargin
)
inputs.fromRoot { root =>
val output = os.proc(TestUtil.cli, extraOptions, fileName, "--js", extraArgs).call(cwd =
root
).out.trim()
expect(output.linesIterator.toSeq.last == message)
}
}
test("simple script JS") {
simpleJsTest()
}
test("simple script JS in release mode") {
simpleJsTest("--js-mode", "release")
}
test("simple script JS command") {
val fileName = "simple.sc"
val message = "Hello"
val inputs = TestInputs(
os.rel / fileName ->
s"""import scala.scalajs.js
|val console = js.Dynamic.global.console
|val msg = "$message"
|console.log(msg)
|""".stripMargin
)
inputs.fromRoot { root =>
val output = os.proc(
TestUtil.cli,
extraOptions,
fileName,
"--js",
"--command",
"--scratch-dir",
root / "stuff"
)
.call(cwd = root).out.trim()
val command = output.linesIterator.toVector
val actualOutput = os.proc(command).call(cwd = root).out.trim()
expect(actualOutput.linesIterator.toSeq.last == message)
}
}
test("esmodule import JS") {
val fileName = "simple.sc"
val message = "Hello"
val inputs = TestInputs(
os.rel / fileName ->
s"""//> using jsModuleKind "es"
|import scala.scalajs.js
|import scala.scalajs.js.annotation._
|
|@js.native
|@JSImport("console", JSImport.Namespace)
|object console extends js.Object {
| def log(msg: js.Any): Unit = js.native
|}
|
|val msg = "$message"
|console.log(msg)
|""".stripMargin
)
inputs.fromRoot { root =>
val output = os.proc(TestUtil.cli, extraOptions, fileName, "--js")
.call(cwd = root).out.trim()
expect(output == message)
}
}
test("simple script JS via config file") {
val message = "Hello"
val inputs = TestInputs(
os.rel / "simple.sc" ->
s"""//> using platform "scala-js"
|import scala.scalajs.js
|val console = js.Dynamic.global.console
|val msg = "$message"
|console.log(msg)
|""".stripMargin
)
inputs.fromRoot { root =>
val output = os.proc(TestUtil.cli, extraOptions, ".").call(cwd = root).out.trim()
expect(output == message)
}
}
test("simple script JS via platform option") {
val message = "Hello"
val inputs = TestInputs(
os.rel / "simple.sc" ->
s"""//> using platform "scala-native"
|import scala.scalajs.js
|val console = js.Dynamic.global.console
|val msg = "$message"
|console.log(msg)
|""".stripMargin
)
inputs.fromRoot { root =>
val output =
os.proc(TestUtil.cli, extraOptions, ".", "--platform", "js").call(cwd = root).out.trim()
expect(output == message)
}
}
def platformNl: String = if (Properties.isWin) "\\r\\n" else "\\n"
def simpleNativeTests(): Unit = {
val fileName = "simple.sc"
val message = "Hello"
val inputs = TestInputs(
os.rel / fileName ->
s"""import scala.scalanative.libc._
|import scala.scalanative.unsafe._
|
|Zone { implicit z =>
| stdio.printf(toCString("$message$platformNl"))
|}
|""".stripMargin
)
inputs.fromRoot { root =>
val output =
os.proc(TestUtil.cli, extraOptions, fileName, "--native", "-q")
.call(cwd = root)
.out.trim()
expect(output == message)
}
}
test("simple script native") {
simpleNativeTests()
}
test("simple script native command") {
val fileName = "simple.sc"
val message = "Hello"
val inputs = TestInputs(
os.rel / fileName ->
s"""import scala.scalanative.libc._
|import scala.scalanative.unsafe._
|
|Zone { implicit z =>
| stdio.printf(toCString("$message$platformNl"))
|}
|""".stripMargin
)
inputs.fromRoot { root =>
val output =
os.proc(TestUtil.cli, extraOptions, fileName, "--native", "--command")
.call(cwd = root)
.out.trim()
val command = output.linesIterator.toVector.filter(!_.startsWith("["))
val actualOutput = os.proc(command).call(cwd = root).out.trim()
expect(actualOutput == message)
}
}
test("Resource embedding in Scala Native") {
val projectDir = "nativeres"
val resourceContent = "resource contents"
val resourceFileName = "embeddedfile.txt"
val inputs = TestInputs(
os.rel / projectDir / "main.scala" ->
s"""|//> using platform "scala-native"
|//> using resourceDir "resources"
|
|import java.nio.charset.StandardCharsets
|import java.io.{BufferedReader, InputStreamReader}
|
|object Main {
| def main(args: Array[String]): Unit = {
| val inputStream = getClass().getResourceAsStream("/$resourceFileName")
| val nativeResourceText = new BufferedReader(
| new InputStreamReader(inputStream, StandardCharsets.UTF_8)
| ).readLine()
| println(nativeResourceText)
| }
|}
|""".stripMargin,
os.rel / projectDir / "resources" / resourceFileName -> resourceContent
)
inputs.fromRoot { root =>
val output =
os.proc(TestUtil.cli, extraOptions, projectDir, "-q")
.call(cwd = root)
.out.trim()
println(output)
expect(output == resourceContent)
}
}
test("Scala Native C Files are correctly handled as a regular Input") {
val projectDir = "native-interop"
val interopFileName = "bindings.c"
val interopMsg = "Hello C!"
val inputs = TestInputs(
os.rel / projectDir / "main.scala" ->
s"""|//> using platform "scala-native"
|
|import scala.scalanative.unsafe._
|
|@extern
|object Bindings {
| @name("scalanative_print")
| def print(): Unit = extern
|}
|
|object Main {
| def main(args: Array[String]): Unit = {
| Bindings.print()
| }
|}
|""".stripMargin,
os.rel / projectDir / interopFileName ->
s"""|#include <stdio.h>
|
|void scalanative_print() {
| printf("$interopMsg\\n");
|}
|""".stripMargin
)
inputs.fromRoot { root =>
val output =
os.proc(TestUtil.cli, extraOptions, projectDir, "-q")
.call(cwd = root)
.out.trim()
expect(output == interopMsg)
os.move(root / projectDir / interopFileName, root / projectDir / "bindings2.c")
val output2 =
os.proc(TestUtil.cli, extraOptions, projectDir, "-q")
.call(cwd = root)
.out.trim()
// LLVM throws linking errors if scalanative_print is internally repeated.
// This can happen if a file containing it will be removed/renamed in src,
// but somehow those changes will not be reflected in the output directory,
// causing symbols inside linked files to be doubled.
// Because of that, the removed file should not be passed to linker,
// otherwise this test will fail.
expect(output2 == interopMsg)
}
}
if (actualScalaVersion.startsWith("3.1"))
test("Scala 3 in Scala Native") {
val message = "using Scala 3 Native"
val fileName = "scala3native.scala"
val inputs = TestInputs(
os.rel / fileName ->
s"""import scala.scalanative.libc._
|import scala.scalanative.unsafe._
|
|@main def main() =
| val message = "$message"
| Zone { implicit z =>
| stdio.printf(toCString(message))
| }
|""".stripMargin
)
inputs.fromRoot { root =>
val output =
os.proc(TestUtil.cli, extraOptions, fileName, "--native", "-q")
.call(cwd = root)
.out.trim()
expect(output == message)
}
}
test("Multiple scripts") {
val message = "Hello"
val inputs = TestInputs(
os.rel / "messages.sc" ->
s"""def msg = "$message"
|""".stripMargin,
os.rel / "print.sc" ->
s"""println(messages.msg)
|""".stripMargin
)
inputs.fromRoot { root =>
val output = os.proc(TestUtil.cli, extraOptions, "print.sc", "messages.sc").call(cwd =
root
).out.trim()
expect(output == message)
}
}
test("main.sc is not a special case") {
val message = "Hello"
val inputs = TestInputs(
os.rel / "main.sc" ->
s"""println("$message")
|""".stripMargin
)
inputs.fromRoot { root =>
val output = os.proc(TestUtil.cli, extraOptions, "main.sc").call(cwd =
root
).out.trim()
expect(output == message)
}
}
test("use method from main.sc file") {
val message = "Hello"
val inputs = TestInputs(
os.rel / "message.sc" ->
s"""println(main.msg)
|""".stripMargin,
os.rel / "main.sc" ->
s"""def msg = "$message"
|""".stripMargin
)
inputs.fromRoot { root =>
val output = os.proc(TestUtil.cli, extraOptions, "message.sc", "main.sc").call(cwd =
root
).out.trim()
expect(output == message)
}
}
test("Multiple scripts JS") {
val message = "Hello"
val inputs = TestInputs(
os.rel / "messages.sc" ->
s"""def msg = "$message"
|""".stripMargin,
os.rel / "print.sc" ->
s"""import scala.scalajs.js
|val console = js.Dynamic.global.console
|console.log(messages.msg)
|""".stripMargin
)
inputs.fromRoot { root =>
val output = os.proc(TestUtil.cli, extraOptions, "print.sc", "messages.sc", "--js").call(cwd =
root
).out.trim()
expect(output == message)
}
}
def multipleScriptsNative(): Unit = {
val message = "Hello"
val inputs = TestInputs(
os.rel / "messages.sc" ->
s"""def msg = "$message"
|""".stripMargin,
os.rel / "print.sc" ->
s"""import scala.scalanative.libc._
|import scala.scalanative.unsafe._
|
|Zone { implicit z =>
| stdio.printf(toCString(messages.msg + "$platformNl"))
|}
|""".stripMargin
)
inputs.fromRoot { root =>
val output =
os.proc(TestUtil.cli, extraOptions, "print.sc", "messages.sc", "--native", "-q")
.call(cwd = root)
.out.trim()
expect(output == message)
}
}
test("Multiple scripts native") {
multipleScriptsNative()
}
test("Directory") {
val message = "Hello"
val inputs = TestInputs(
os.rel / "dir" / "messages.sc" ->
s"""def msg = "$message"
|""".stripMargin,
os.rel / "dir" / "print.sc" ->
s"""println(messages.msg)
|""".stripMargin
)
inputs.fromRoot { root =>
val output = os.proc(TestUtil.cli, extraOptions, "dir", "--main-class", "print_sc").call(cwd =
root
).out.trim()
expect(output == message)
}
}
test("No default inputs when the `run` sub-command is launched with no args") {
val inputs = TestInputs(
os.rel / "dir" / "print.sc" ->
s"""println("Foo")
|""".stripMargin
)
inputs.fromRoot { root =>
val res = os.proc(TestUtil.cli, "run", extraOptions, "--main-class", "print")
.call(cwd = root / "dir", check = false, mergeErrIntoOut = true)
val output = res.out.trim()
expect(res.exitCode != 0)
expect(output.contains("No inputs provided"))
}
}
test("Debugging") {
val inputs = TestInputs(
os.rel / "Foo.scala" ->
s"""object Foo {
| def main(args: Array[String]): Unit = {
| println("foo")
| }
|}
|""".stripMargin
)
inputs.fromRoot { root =>
val out1 = os.proc(TestUtil.cli, "run", extraOptions, ".", "--debug", "--command")
.call(cwd = root).out.trim().lines.toList.asScala
val out2 = os.proc(
TestUtil.cli,
"run",
extraOptions,
".",
"--debug-port",
"5006",
"--debug-mode",
"listen",
"--command"
).call(cwd = root).out.trim().lines.toList.asScala
def debugString(server: String, port: String) =
s"-agentlib:jdwp=transport=dt_socket,server=$server,suspend=y,address=$port"
assert(out1.exists(_ == debugString("y", "5005")))
assert(out2.exists(_ == debugString("n", "5006")))
}
}
test("Pass arguments") {
val inputs = TestInputs(
os.rel / "Test.scala" ->
s"""object Test {
| def main(args: Array[String]): Unit = {
| println(args(0))
| }
|}
|""".stripMargin
)
val message = "Hello"
inputs.fromRoot { root =>
val output = os.proc(TestUtil.cli, "run", extraOptions, ".", "--", message)
.call(cwd = root)
.out.trim()
expect(output == message)
}
}
def passArgumentsScala3(): Unit = {
val inputs = TestInputs(
os.rel / "Test.scala" ->
s"""object Test:
| def main(args: Array[String]): Unit =
| val message = args(0)
| println(message)
|""".stripMargin
)
val message = "Hello"
inputs.fromRoot { root =>
val output = os.proc(TestUtil.cli, "run", extraOptions, ".", "--", message)
.call(cwd = root)
.out.trim()
expect(output == message)
}
}
if (actualScalaVersion.startsWith("3."))
test("Pass arguments - Scala 3") {
passArgumentsScala3()
}
test("Directory JS") {
val message = "Hello"
val inputs = TestInputs(
os.rel / "dir" / "messages.sc" ->
s"""def msg = "$message"
|""".stripMargin,
os.rel / "dir" / "print.sc" ->
s"""import scala.scalajs.js
|val console = js.Dynamic.global.console
|console.log(messages.msg)
|""".stripMargin
)
inputs.fromRoot { root =>
val output = os.proc(TestUtil.cli, extraOptions, "dir", "--js", "--main-class", "print_sc")
.call(cwd = root)
.out.trim()
expect(output == message)
}
}
def directoryNative(): Unit = {
val message = "Hello"
val inputs = TestInputs(
os.rel / "dir" / "messages.sc" ->
s"""def msg = "$message"
|""".stripMargin,
os.rel / "dir" / "print.sc" ->
s"""import scala.scalanative.libc._
|import scala.scalanative.unsafe._
|
|Zone { implicit z =>
| stdio.printf(toCString(messages.msg + "$platformNl"))
|}
|""".stripMargin
)
inputs.fromRoot { root =>
val output =
os.proc(TestUtil.cli, extraOptions, "dir", "--native", "--main-class", "print_sc", "-q")
.call(cwd = root)
.out.trim()
expect(output == message)
}
}
// TODO: make nice messages that the scenario is unsupported with 2.12
if (actualScalaVersion.startsWith("2.13"))
test("Directory native") {
directoryNative()
}
test("sub-directory") {
val fileName = "script.sc"
val expectedClassName = fileName.stripSuffix(".sc") + "$"
val scriptPath = os.rel / "something" / fileName
val inputs = TestInputs(
scriptPath ->
s"""println(getClass.getName)
|""".stripMargin
)
inputs.fromRoot { root =>
val output = os.proc(TestUtil.cli, extraOptions, scriptPath.toString)
.call(cwd = root)
.out.text()
.trim
expect(output == expectedClassName)
}
}
test("sub-directory and script") {
val fileName = "script.sc"
val expectedClassName = fileName.stripSuffix(".sc") + "$"
val scriptPath = os.rel / "something" / fileName
val inputs = TestInputs(
os.rel / "dir" / "Messages.scala" ->
s"""object Messages {
| def msg = "Hello"
|}
|""".stripMargin,
os.rel / "dir" / "Print.scala" ->
s"""object Print {
| def main(args: Array[String]): Unit =
| println(Messages.msg)
|}
|""".stripMargin,
scriptPath ->
s"""println(getClass.getName)
|""".stripMargin
)
inputs.fromRoot { root =>
val output = os.proc(TestUtil.cli, extraOptions, "dir", scriptPath.toString)
.call(cwd = root)
.out.text()
.trim
expect(output == expectedClassName)
}
}
test("setting root dir with no inputs") {
val url = "https://gist.github.com/alexarchambault/7b4ec20c4033690dd750ffd601e540ec"
emptyInputs.fromRoot { root =>
os.proc(TestUtil.cli, extraOptions, escapedUrls(url)).call(cwd = root)
expect(os.exists(root / ".scala-build"))
}
}
private lazy val ansiRegex = "\u001B\\[[;\\d]*m".r
private def stripAnsi(s: String): String =
ansiRegex.replaceAllIn(s, "")
test("stack traces") {
val inputs = TestInputs(
os.rel / "Throws.scala" ->
s"""object Throws {
| def something(): String =
| sys.error("nope")
| def main(args: Array[String]): Unit =
| try something()
| catch {
| case e: Exception =>
| throw new Exception("Caught exception during processing", e)
| }
|}
|""".stripMargin
)
inputs.fromRoot { root =>
// format: off
val cmd = Seq[os.Shellable](
TestUtil.cli, "run", extraOptions, ".",
"--java-prop", "scala.colored-stack-traces=false"
)
// format: on
val res = os.proc(cmd).call(cwd = root, check = false, mergeErrIntoOut = true)
val output = res.out.lines()
// FIXME We need to have the pretty-stacktraces stuff take scala.colored-stack-traces into account
val exceptionLines =
output.map(stripAnsi).dropWhile(!_.startsWith("Exception in thread "))
val tab = "\t"
val expectedLines =
if (actualScalaVersion.startsWith("2.12."))
s"""Exception in thread "main" java.lang.Exception: Caught exception during processing
|${tab}at Throws$$.main(Throws.scala:8)
|${tab}at Throws.main(Throws.scala)
|Caused by: java.lang.RuntimeException: nope
|${tab}at scala.sys.package$$.error(package.scala:30)
|${tab}at Throws$$.something(Throws.scala:3)
|${tab}at Throws$$.main(Throws.scala:5)
|$tab... 1 more
|""".stripMargin.linesIterator.toVector
else if (actualScalaVersion.startsWith("3.") || actualScalaVersion.startsWith("2.13."))
s"""Exception in thread "main" java.lang.Exception: Caught exception during processing
|${tab}at Throws$$.main(Throws.scala:8)
|${tab}at Throws.main(Throws.scala)
|Caused by: java.lang.RuntimeException: nope
|${tab}at scala.sys.package$$.error(package.scala:27)
|${tab}at Throws$$.something(Throws.scala:3)
|${tab}at Throws$$.main(Throws.scala:5)
|$tab... 1 more
|""".stripMargin.linesIterator.toVector
else
sys.error(s"Unexpected Scala version: $actualScalaVersion")
if (exceptionLines != expectedLines) {
pprint.log(exceptionLines)
pprint.log(expectedLines)
}
assert(exceptionLines == expectedLines, clues(output))
}
}
def stackTraceInScriptScala2(): Unit = {
val inputs = TestInputs(
os.rel / "throws.sc" ->
s"""def something(): String =
| sys.error("nope")
|try something()
|catch {
| case e: Exception =>
| throw new Exception("Caught exception during processing", e)
|}
|""".stripMargin
)
inputs.fromRoot { root =>
// format: off
val cmd = Seq[os.Shellable](
TestUtil.cli, "run", extraOptions, ".",
"--java-prop", "scala.colored-stack-traces=false"
)
// format: on
val res = os.proc(cmd).call(cwd = root, check = false, mergeErrIntoOut = true)
val output = res.out.lines()
val exceptionLines = output.dropWhile(!_.startsWith("Exception in thread "))
val tab = "\t"
val expectedLines =
if (actualScalaVersion.startsWith("2.12."))
s"""Exception in thread "main" java.lang.ExceptionInInitializerError
|${tab}at throws_sc$$.main(throws.sc:24)
|${tab}at throws_sc.main(throws.sc)
|Caused by: java.lang.Exception: Caught exception during processing
|${tab}at throws$$.<init>(throws.sc:6)
|${tab}at throws$$.<clinit>(throws.sc)
|$tab... 2 more
|Caused by: java.lang.RuntimeException: nope
|${tab}at scala.sys.package$$.error(package.scala:30)
|${tab}at throws$$.something(throws.sc:2)
|${tab}at throws$$.<init>(throws.sc:3)
|$tab... 3 more""".stripMargin.linesIterator.toVector
else
s"""Exception in thread "main" java.lang.ExceptionInInitializerError
|${tab}at throws_sc$$.main(throws.sc:24)
|${tab}at throws_sc.main(throws.sc)
|Caused by: java.lang.Exception: Caught exception during processing
|${tab}at throws$$.<clinit>(throws.sc:6)
|$tab... 2 more
|Caused by: java.lang.RuntimeException: nope
|${tab}at scala.sys.package$$.error(package.scala:27)
|${tab}at throws$$.something(throws.sc:2)
|${tab}at throws$$.<clinit>(throws.sc:3)
|$tab... 2 more
|""".stripMargin.linesIterator.toVector
if (exceptionLines != expectedLines) {
println(exceptionLines.mkString("\n"))
println(expectedLines)
}
assert(
exceptionLines.length == expectedLines.length,
clues(output, exceptionLines.length, expectedLines.length)
)
for (i <- exceptionLines.indices)
assert(
exceptionLines(i) == expectedLines(i),
clues(output, exceptionLines(i), expectedLines(i))
)
}
}
if (actualScalaVersion.startsWith("2."))
test("stack traces in script") {
stackTraceInScriptScala2()
}
def scriptStackTraceScala3(): Unit = {
val inputs = TestInputs(
os.rel / "throws.sc" ->
s"""def something(): String =
| val message = "nope"
| sys.error(message)
|
|try something()
|catch {
| case e: Exception =>
| throw new Exception("Caught exception during processing", e)
|}
|""".stripMargin
)
inputs.fromRoot { root =>
// format: off
val cmd = Seq[os.Shellable](
TestUtil.cli, "run", extraOptions, ".")
// format: on
val res = os.proc(cmd).call(cwd = root, check = false, mergeErrIntoOut = true)
val output = res.out.lines()
val exceptionLines = output
.map(stripAnsi)
.dropWhile(!_.startsWith("Exception in thread "))
val tab = "\t"
val expectedLines =
s"""Exception in thread "main" java.lang.ExceptionInInitializerError
|${tab}at throws_sc$$.main(throws.sc:26)
|${tab}at throws_sc.main(throws.sc)
|Caused by: java.lang.Exception: Caught exception during processing
|${tab}at throws$$.<clinit>(throws.sc:8)
|$tab... 2 more
|Caused by: java.lang.RuntimeException: nope
|${tab}at scala.sys.package$$.error(package.scala:27)
|${tab}at throws$$.something(throws.sc:3)
|${tab}at throws$$.<clinit>(throws.sc:5)
|$tab... 2 more""".stripMargin.linesIterator.toVector
assert(
exceptionLines.length == expectedLines.length,
clues(output, exceptionLines.length, expectedLines.length)
)
for (i <- exceptionLines.indices)
assert(
exceptionLines(i) == expectedLines(i),
clues(output, exceptionLines(i), expectedLines(i))
)
}
}
if (actualScalaVersion.startsWith("3."))
test("stack traces in script in Scala 3") {
scriptStackTraceScala3()
}
val emptyInputs: TestInputs = TestInputs(os.rel / ".placeholder" -> "")
def piping(): Unit = {
emptyInputs.fromRoot { root =>
val cliCmd = (TestUtil.cli ++ extraOptions).mkString(" ")
val cmd = s""" echo 'println("Hello" + " from pipe")' | $cliCmd _.sc """
val res = os.proc("bash", "-c", cmd).call(cwd = root)
val expectedOutput = "Hello from pipe" + System.lineSeparator()
expect(res.out.text() == expectedOutput)
}
}
if (!Properties.isWin) {
test("piping") {
piping()
}
test("Scripts accepted as piped input") {
val message = "Hello"
val input = s"println(\"$message\")"
emptyInputs.fromRoot { root =>
val output = os.proc(TestUtil.cli, "-", extraOptions)
.call(cwd = root, stdin = input)
.out.trim()
expect(output == message)
}
}
test("Scala code accepted as piped input") {
val expectedOutput = "Hello"
val pipedInput = s"object Test extends App { println(\"$expectedOutput\") }"
emptyInputs.fromRoot { root =>
val output = os.proc(TestUtil.cli, "_.scala", extraOptions)
.call(cwd = root, stdin = pipedInput)
.out.trim()
expect(output == expectedOutput)
}
}
test("Scala code with references to existing files accepted as piped input") {
val expectedOutput = "Hello"
val pipedInput =
s"""object Test extends App {
| val data = SomeData(value = "$expectedOutput")
| println(data.value)
|}""".stripMargin
val inputs = TestInputs(os.rel / "SomeData.scala" -> "case class SomeData(value: String)")
inputs.fromRoot { root =>
val output = os.proc(TestUtil.cli, ".", "_.scala", extraOptions)
.call(cwd = root, stdin = pipedInput)
.out.trim()
expect(output == expectedOutput)
}
}
test("Java code accepted as piped input") {
val expectedOutput = "Hello"
val pipedInput =
s"""public class Main {
| public static void main(String[] args) {
| System.out.println("$expectedOutput");
| }
|}
|""".stripMargin
emptyInputs.fromRoot { root =>
val output = os.proc(TestUtil.cli, "_.java", extraOptions)
.call(cwd = root, stdin = pipedInput)
.out.trim()
expect(output == expectedOutput)
}
}
test("Java code with multiple classes accepted as piped input") {
val expectedOutput = "Hello"
val pipedInput =
s"""class OtherClass {
| public String message;
| public OtherClass(String message) {
| this.message = message;
| }
|}
|
|public class Main {
| public static void main(String[] args) {
| OtherClass obj = new OtherClass("$expectedOutput");
| System.out.println(obj.message);
| }
|}
|""".stripMargin
emptyInputs.fromRoot { root =>
val output = os.proc(TestUtil.cli, "_.java", extraOptions)
.call(cwd = root, stdin = pipedInput)
.out.trim()
expect(output == expectedOutput)
}
}
test(
"snippets mixed with piped Scala code and existing sources allow for cross-references"
) {
val hello = "Hello"
val comma = ", "
val world = "World"
val exclamation = "!"
val expectedOutput = hello + comma + world + exclamation
val scriptSnippet = s"def world = \"$world\""
val scalaSnippet = "case class ScalaSnippetData(value: String)"
val javaSnippet =
s"public class JavaSnippet { public static String exclamation = \"$exclamation\"; }"
val pipedInput = s"def hello = \"$hello\""
val inputs =
TestInputs(os.rel / "Main.scala" ->
s"""object Main extends App {
| val hello = stdin.hello
| val comma = ScalaSnippetData(value = "$comma").value