|
6 | 6 | package io.openapiprocessor.core
|
7 | 7 |
|
8 | 8 | import io.kotest.core.spec.style.StringSpec
|
| 9 | +import io.kotest.engine.spec.tempdir |
9 | 10 | import io.kotest.matchers.booleans.shouldBeTrue
|
10 | 11 | import io.openapiprocessor.core.parser.ParserType
|
11 | 12 | import io.openapiprocessor.test.*
|
12 |
| -import java.io.ByteArrayOutputStream |
13 |
| -import java.io.OutputStream |
14 |
| -import java.net.URI |
15 |
| -import java.nio.file.Path |
16 |
| -import java.util.* |
17 |
| -import javax.tools.* |
18 | 13 |
|
19 | 14 |
|
20 | 15 | class CompileExpectedSpec: StringSpec({
|
21 | 16 |
|
22 |
| - class MemoryFile(name: String, kind: JavaFileObject.Kind) |
23 |
| - : SimpleJavaFileObject(URI("string:///${name}"), kind) { |
24 |
| - |
25 |
| - override fun openOutputStream(): OutputStream { |
26 |
| - return ByteArrayOutputStream() |
27 |
| - } |
28 |
| - } |
29 |
| - |
30 |
| - class MemoryFileManager(fileManager: StandardJavaFileManager) |
31 |
| - : ForwardingJavaFileManager<StandardJavaFileManager>(fileManager) { |
32 |
| - |
33 |
| - override fun getJavaFileForOutput( |
34 |
| - location: JavaFileManager.Location, |
35 |
| - className: String, |
36 |
| - kind: JavaFileObject.Kind, |
37 |
| - sibling: FileObject |
38 |
| - ): JavaFileObject { |
39 |
| - return MemoryFile(className, kind) |
40 |
| - } |
41 |
| - |
42 |
| - fun getJavaFileObjectsFromPaths(paths: Iterable<Path>): Iterable<JavaFileObject> { |
43 |
| - return fileManager.getJavaFileObjectsFromPaths(paths) |
44 |
| - } |
45 |
| - } |
46 |
| - |
47 | 17 | for (testSet in sources()) {
|
48 | 18 | "compile - $testSet".config(enabled = true) {
|
49 |
| - val itemsReader = TestItemsReader(ResourceReader(CompileExpectedSpec::class.java)) |
50 |
| - |
51 |
| - val source = testSet.name |
52 |
| - val sourcePath = "/tests/$source" |
53 |
| - |
54 |
| - val compilePaths = mutableListOf<Path>() |
55 |
| - |
56 |
| - // stuff used by all tests |
57 |
| - compilePaths.add(Path.of("src/testInt/resources/compile/Generated.java")) |
58 |
| - compilePaths.add(Path.of("src/testInt/resources/compile/Mapping.java")) |
59 |
| - compilePaths.add(Path.of("src/testInt/resources/compile/Parameter.java")) |
60 |
| - |
61 |
| - var expected = itemsReader.read(sourcePath, "outputs.yaml").items |
62 |
| - expected = expected.filter { ! it.endsWith("properties") } |
63 |
| - val expectedFileNames = expected.map { it.replaceFirst("<model>", "_${testSet.modelType}_") } |
64 |
| - expectedFileNames.forEach { |
65 |
| - compilePaths.add(Path.of("src/testInt/resources${sourcePath}/$it")) |
66 |
| - } |
67 |
| - |
68 |
| - if (itemsReader.exists(sourcePath, "compile.yaml")) { |
69 |
| - val additionalFileNames = itemsReader.read(sourcePath, "compile.yaml").items |
70 |
| - |
71 |
| - additionalFileNames.forEach { |
72 |
| - if (it.startsWith("not ")) { |
73 |
| - compilePaths.remove(Path.of("src/testInt/resources/${it.substring(4)}")) |
74 |
| - } else { |
75 |
| - compilePaths.add(Path.of("src/testInt/resources/$it")) |
76 |
| - } |
77 |
| - } |
78 |
| - } |
79 |
| - |
80 |
| - val diagnostics = DiagnosticCollector<JavaFileObject>() |
81 |
| - val compiler = ToolProvider.getSystemJavaCompiler() |
82 |
| - val manager = MemoryFileManager(compiler.getStandardFileManager(diagnostics, null, null)) |
| 19 | + val folder = tempdir() |
| 20 | + val reader = ResourceReader(CompileExpectedSpec::class.java) |
83 | 21 |
|
84 |
| - val options = listOf<String>() |
85 |
| - val compilationUnit = manager.getJavaFileObjectsFromPaths(compilePaths) |
86 |
| - val task = compiler.getTask(null, manager, diagnostics, options, null, compilationUnit) |
87 |
| - val success = task.call() |
88 |
| - if(!success) { |
89 |
| - for (diagnostic in diagnostics.diagnostics) { |
90 |
| - println("CompileSpec: compile error at ${diagnostic.source.name}:${diagnostic.lineNumber}, ${diagnostic.getMessage(Locale.ENGLISH)}") |
91 |
| - } |
92 |
| - } |
| 22 | + val testFiles = TestFilesNative(folder, reader) |
93 | 23 |
|
94 |
| - success.shouldBeTrue() |
| 24 | + TestSetCompiler(testSet, testFiles) |
| 25 | + .run() |
| 26 | + .shouldBeTrue() |
95 | 27 | }
|
96 | 28 | }
|
97 | 29 | })
|
|
0 commit comments