Skip to content

Commit b15959c

Browse files
update
1 parent 7dc67c8 commit b15959c

File tree

2 files changed

+49
-7
lines changed
  • codegen-core/src
    • main/kotlin/software/amazon/smithy/rust/codegen/core/testutil
    • test/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators

2 files changed

+49
-7
lines changed

codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/testutil/Rust.kt

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ import java.nio.file.Files.createTempDirectory
4242
import java.nio.file.Path
4343
import kotlin.io.path.absolutePathString
4444

45+
object Commands {
46+
val CargoEnvDWarnings = mapOf(
47+
"RUSTFLAGS" to "-D warnings --cfg aws_sdk_unstable",
48+
)
49+
val CargoEnvDDeadCode = mapOf(
50+
"RUSTFLAGS" to "-A dead_code --cfg aws_sdk_unstable",
51+
)
52+
const val CargoTest = "cargo test --all-features"
53+
const val CargoCheck = "cargo check --all-features"
54+
const val CargoFmt = "cargo fmt "
55+
const val CargoClippy = "cargo clippy"
56+
}
57+
4558
val TestModuleDocProvider = object : ModuleDocProvider {
4659
override fun docsWriter(module: RustModule.LeafModule): Writable = writable {
4760
docs("Some test documentation\n\nSome more details...")
@@ -332,14 +345,14 @@ fun TestWriterDelegator.compileAndTest(
332345
println("Generated files:")
333346
printGeneratedFiles()
334347
try {
335-
"cargo fmt".runCommand(baseDir)
348+
Commands.CargoFmt.runCommand(baseDir)
336349
} catch (e: Exception) {
337350
// cargo fmt errors are useless, ignore
338351
}
339-
val env = mapOf("RUSTFLAGS" to "-A dead_code")
340-
val testOutput = "cargo test".runCommand(baseDir, env)
352+
val env = Commands.CargoEnvDDeadCode
353+
val testOutput = Commands.CargoTest.runCommand(baseDir, env)
341354
if (runClippy) {
342-
"cargo clippy".runCommand(baseDir, env)
355+
Commands.CargoClippy.runCommand(baseDir, env)
343356
}
344357
return testOutput
345358
}
@@ -379,9 +392,9 @@ fun RustWriter.compileAndTest(
379392
val testModule = tempDir.resolve("src/$module.rs")
380393
try {
381394
val testOutput = if ((mainRs.readText() + testModule.readText()).contains("#[test]")) {
382-
"cargo test".runCommand(tempDir.toPath())
395+
Commands.CargoTest.runCommand(tempDir.toPath())
383396
} else {
384-
"cargo check".runCommand(tempDir.toPath())
397+
Commands.CargoCheck.runCommand(tempDir.toPath())
385398
}
386399
if (expectFailure) {
387400
println("Test sources for debugging: file://${testModule.absolutePath}")
@@ -488,4 +501,4 @@ fun TestWriterDelegator.unitTest(test: Writable): TestWriterDelegator {
488501
return this
489502
}
490503

491-
fun String.runWithWarnings(crate: Path) = this.runCommand(crate, mapOf("RUSTFLAGS" to "-D warnings"))
504+
fun String.runWithWarnings(crate: Path) = this.runCommand(crate, Commands.CargoEnvDWarnings)

codegen-core/src/test/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/BuilderGeneratorTest.kt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@ import software.amazon.smithy.rust.codegen.core.testutil.TestWorkspace
1818
import software.amazon.smithy.rust.codegen.core.testutil.compileAndTest
1919
import software.amazon.smithy.rust.codegen.core.testutil.testSymbolProvider
2020
import software.amazon.smithy.rust.codegen.core.testutil.unitTest
21+
import kotlin.io.path.extension
22+
import kotlin.io.path.readText
2123

2224
internal class BuilderGeneratorTest {
2325
private val model = StructureGeneratorTest.model
2426
private val inner = StructureGeneratorTest.inner
2527
private val struct = StructureGeneratorTest.struct
2628
private val credentials = StructureGeneratorTest.credentials
2729
private val secretStructure = StructureGeneratorTest.secretStructure
30+
private val errorStruct = StructureGeneratorTest.error
2831

2932
@Test
3033
fun `generate builders`() {
@@ -137,4 +140,30 @@ internal class BuilderGeneratorTest {
137140
}
138141
project.compileAndTest()
139142
}
143+
144+
@Test
145+
fun `don't add serde to error types`() {
146+
val provider = testSymbolProvider(model)
147+
val project = TestWorkspace.testProject(provider)
148+
project.moduleFor(errorStruct) {
149+
rust("##![allow(deprecated)]")
150+
StructureGenerator(model, provider, this, errorStruct, emptyList()).render()
151+
implBlock(provider.toSymbol(errorStruct)) {
152+
BuilderGenerator.renderConvenienceMethod(this, provider, errorStruct)
153+
}
154+
}
155+
project.withModule(provider.moduleForBuilder(errorStruct)) {
156+
BuilderGenerator(model, provider, errorStruct, emptyList()).render(this)
157+
}
158+
project.compileAndTest()
159+
160+
// checks if there is a serde derive in the code
161+
project.generatedFiles().forEach {
162+
if (it.extension == "rs") {
163+
val file = project.baseDir.resolve(it).toFile().readText()
164+
val check = file.contains("derive(serde::Deserialize)") || file.contains("derive(serde::Serialize)")
165+
assert(!check)
166+
}
167+
}
168+
}
140169
}

0 commit comments

Comments
 (0)