Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-k-cameron committed Jun 20, 2023
1 parent 7dc67c8 commit b15959c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ import java.nio.file.Files.createTempDirectory
import java.nio.file.Path
import kotlin.io.path.absolutePathString

object Commands {
val CargoEnvDWarnings = mapOf(
"RUSTFLAGS" to "-D warnings --cfg aws_sdk_unstable",
)
val CargoEnvDDeadCode = mapOf(
"RUSTFLAGS" to "-A dead_code --cfg aws_sdk_unstable",
)
const val CargoTest = "cargo test --all-features"
const val CargoCheck = "cargo check --all-features"
const val CargoFmt = "cargo fmt "
const val CargoClippy = "cargo clippy"
}

val TestModuleDocProvider = object : ModuleDocProvider {
override fun docsWriter(module: RustModule.LeafModule): Writable = writable {
docs("Some test documentation\n\nSome more details...")
Expand Down Expand Up @@ -332,14 +345,14 @@ fun TestWriterDelegator.compileAndTest(
println("Generated files:")
printGeneratedFiles()
try {
"cargo fmt".runCommand(baseDir)
Commands.CargoFmt.runCommand(baseDir)
} catch (e: Exception) {
// cargo fmt errors are useless, ignore
}
val env = mapOf("RUSTFLAGS" to "-A dead_code")
val testOutput = "cargo test".runCommand(baseDir, env)
val env = Commands.CargoEnvDDeadCode
val testOutput = Commands.CargoTest.runCommand(baseDir, env)
if (runClippy) {
"cargo clippy".runCommand(baseDir, env)
Commands.CargoClippy.runCommand(baseDir, env)
}
return testOutput
}
Expand Down Expand Up @@ -379,9 +392,9 @@ fun RustWriter.compileAndTest(
val testModule = tempDir.resolve("src/$module.rs")
try {
val testOutput = if ((mainRs.readText() + testModule.readText()).contains("#[test]")) {
"cargo test".runCommand(tempDir.toPath())
Commands.CargoTest.runCommand(tempDir.toPath())
} else {
"cargo check".runCommand(tempDir.toPath())
Commands.CargoCheck.runCommand(tempDir.toPath())
}
if (expectFailure) {
println("Test sources for debugging: file://${testModule.absolutePath}")
Expand Down Expand Up @@ -488,4 +501,4 @@ fun TestWriterDelegator.unitTest(test: Writable): TestWriterDelegator {
return this
}

fun String.runWithWarnings(crate: Path) = this.runCommand(crate, mapOf("RUSTFLAGS" to "-D warnings"))
fun String.runWithWarnings(crate: Path) = this.runCommand(crate, Commands.CargoEnvDWarnings)
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ import software.amazon.smithy.rust.codegen.core.testutil.TestWorkspace
import software.amazon.smithy.rust.codegen.core.testutil.compileAndTest
import software.amazon.smithy.rust.codegen.core.testutil.testSymbolProvider
import software.amazon.smithy.rust.codegen.core.testutil.unitTest
import kotlin.io.path.extension
import kotlin.io.path.readText

internal class BuilderGeneratorTest {
private val model = StructureGeneratorTest.model
private val inner = StructureGeneratorTest.inner
private val struct = StructureGeneratorTest.struct
private val credentials = StructureGeneratorTest.credentials
private val secretStructure = StructureGeneratorTest.secretStructure
private val errorStruct = StructureGeneratorTest.error

@Test
fun `generate builders`() {
Expand Down Expand Up @@ -137,4 +140,30 @@ internal class BuilderGeneratorTest {
}
project.compileAndTest()
}

@Test
fun `don't add serde to error types`() {
val provider = testSymbolProvider(model)
val project = TestWorkspace.testProject(provider)
project.moduleFor(errorStruct) {
rust("##![allow(deprecated)]")
StructureGenerator(model, provider, this, errorStruct, emptyList()).render()
implBlock(provider.toSymbol(errorStruct)) {
BuilderGenerator.renderConvenienceMethod(this, provider, errorStruct)
}
}
project.withModule(provider.moduleForBuilder(errorStruct)) {
BuilderGenerator(model, provider, errorStruct, emptyList()).render(this)
}
project.compileAndTest()

// checks if there is a serde derive in the code
project.generatedFiles().forEach {
if (it.extension == "rs") {
val file = project.baseDir.resolve(it).toFile().readText()
val check = file.contains("derive(serde::Deserialize)") || file.contains("derive(serde::Serialize)")
assert(!check)
}
}
}
}

0 comments on commit b15959c

Please sign in to comment.