|
| 1 | +/* |
| 2 | + * Copyright 2023 https://github.com/openapi-processor/openapi-processor-core |
| 3 | + * PDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | +package io.openapiprocessor.core |
| 6 | + |
| 7 | +import io.kotest.core.spec.style.StringSpec |
| 8 | +import io.kotest.matchers.booleans.shouldBeTrue |
| 9 | +import io.openapiprocessor.core.parser.ParserType |
| 10 | +import io.openapiprocessor.test.FileSupport |
| 11 | +import io.openapiprocessor.test.TestSet |
| 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 | + |
| 19 | + |
| 20 | +class CompileExpectedSpec: StringSpec({ |
| 21 | + |
| 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 | + for (testSet in sources()) { |
| 48 | + "compile - $testSet".config(enabled = false) { |
| 49 | + val support = FileSupport( |
| 50 | + CompileExpectedSpec::class.java, |
| 51 | + testSet.inputs, testSet.outputs |
| 52 | + ) |
| 53 | + |
| 54 | + val diagnostics = DiagnosticCollector<JavaFileObject>() |
| 55 | + val compiler = ToolProvider.getSystemJavaCompiler() |
| 56 | + val manager = MemoryFileManager(compiler.getStandardFileManager(diagnostics, null, null)) |
| 57 | + |
| 58 | + val source = testSet.name |
| 59 | + val sourcePath = "/tests/$source" |
| 60 | + val expected = support.readTestItems(sourcePath, "outputs.yaml").items |
| 61 | + |
| 62 | + val expectedFileNames = expected.map { it.replaceFirst("<model>", "model/${testSet.modelType}") } |
| 63 | + val additionalFileNames = support.readTestItems(sourcePath, "compile.yaml").items |
| 64 | + |
| 65 | + val compilePaths = mutableListOf<Path>() |
| 66 | + expectedFileNames.forEach { |
| 67 | + compilePaths.add(Path.of("src/testInt/resources${sourcePath}/$it")) |
| 68 | + } |
| 69 | + additionalFileNames.forEach { |
| 70 | + compilePaths.add(Path.of("src/testInt/resources/$it")) |
| 71 | + } |
| 72 | + |
| 73 | + val options = listOf<String>() |
| 74 | + val compilationUnit = manager.getJavaFileObjectsFromPaths(compilePaths) |
| 75 | + val task = compiler.getTask(null, manager, diagnostics, options, null, compilationUnit) |
| 76 | + val success = task.call() |
| 77 | + if(!success) { |
| 78 | + for (diagnostic in diagnostics.diagnostics) { |
| 79 | + println("CompileSpec: compile error at ${diagnostic.source.name}:${diagnostic.lineNumber}, ${diagnostic.getMessage(Locale.ENGLISH)}") |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + success.shouldBeTrue() |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | +}) |
| 88 | + |
| 89 | +private fun sources(): Collection<TestSet> { |
| 90 | + val compile30 = ALL_30.map { |
| 91 | + testSet(it.name, ParserType.INTERNAL, it.openapi, model = "default", outputs = it.outputs, expected = it.expected) |
| 92 | + } |
| 93 | + |
| 94 | + val compile31 = ALL_31.map { |
| 95 | + testSet(it.name, ParserType.INTERNAL, it.openapi, model = "default", outputs = it.outputs, expected = it.expected) |
| 96 | + } |
| 97 | + |
| 98 | + val compile30r = ALL_30.filter { it.modelTypes.contains(ModelTypes.RECORD) }.map { |
| 99 | + testSet(it.name, ParserType.INTERNAL, it.openapi, model = "record", outputs = it.outputs, expected = it.expected) |
| 100 | + } |
| 101 | + |
| 102 | + val compile31r = ALL_31.filter { it.modelTypes.contains(ModelTypes.RECORD) }.map { |
| 103 | + testSet(it.name, ParserType.INTERNAL, it.openapi, model = "record", outputs = it.outputs, expected = it.expected) |
| 104 | + } |
| 105 | + |
| 106 | + return compile30 + compile31 + compile30r + compile31r |
| 107 | +} |
0 commit comments