Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring and adding aws_sdk_unstable to RUSTFLAG on one of the kotlin test #2804

Open
wants to merge 36 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
8565bf2
test
thomas-k-cameron Jun 22, 2023
f578b86
Update codegen-core/src/main/kotlin/software/amazon/smithy/rust/codeg…
thomas-k-cameron Jun 22, 2023
268b876
Merge branch 'main' into RFC30/test2
thomas-k-cameron Jun 27, 2023
d614f5f
Rust.kt
thomas-k-cameron Jun 29, 2023
31c16bd
Merge branch 'RFC30/test2' of https://github.com/thomas-k-cameron/smi…
thomas-k-cameron Jun 29, 2023
0df1dfb
Merge branch 'main' into RFC30/test2
thomas-k-cameron Jun 29, 2023
e6512fb
asdf
thomas-k-cameron Jun 29, 2023
3624b37
pre-commit
thomas-k-cameron Jun 29, 2023
e0311c3
check
thomas-k-cameron Jun 29, 2023
6e87b13
Update codegen-core/src/main/kotlin/software/amazon/smithy/rust/codeg…
thomas-k-cameron Jun 29, 2023
2f25042
Update codegen-core/src/main/kotlin/software/amazon/smithy/rust/codeg…
thomas-k-cameron Jun 29, 2023
7e06e0c
FIX
thomas-k-cameron Jun 29, 2023
1acf514
Merge branch 'main' into RFC30/test2
thomas-k-cameron Jun 29, 2023
84c943d
fix
thomas-k-cameron Jun 29, 2023
c358294
fix
thomas-k-cameron Jun 29, 2023
99113b5
enable by default
thomas-k-cameron Jun 29, 2023
1e4ec95
Merge branch 'main' into RFC30/test2
thomas-k-cameron Jun 29, 2023
5a36064
fix
thomas-k-cameron Jun 29, 2023
2b24f9d
Merge branch 'RFC30/test2' of https://github.com/thomas-k-cameron/smi…
thomas-k-cameron Jun 29, 2023
970d4c2
Update Rust.kt
thomas-k-cameron Jul 4, 2023
cc33514
Update Rust.kt
thomas-k-cameron Jul 4, 2023
8d78bd4
fix
thomas-k-cameron Jul 6, 2023
7d80faf
better docs
thomas-k-cameron Jul 6, 2023
5756a41
update
thomas-k-cameron Jul 6, 2023
01a69fc
Merge branch 'main' into RFC30/test2
thomas-k-cameron Jul 6, 2023
0dca49e
Merge branches 'RFC30/test2' and 'RFC30/test2' of https://github.com/…
thomas-k-cameron Jul 6, 2023
1496901
fix
thomas-k-cameron Jul 6, 2023
73b40a4
fix
thomas-k-cameron Jul 6, 2023
2647de9
update
thomas-k-cameron Jul 6, 2023
16b51e3
Merge branch 'main' into RFC30/test2
thomas-k-cameron Jul 10, 2023
e7d4597
Merge branch 'main' into RFC30/test2
thomas-k-cameron Aug 17, 2023
5b7d9d1
Merge branch 'main' into RFC30/test2
thomas-k-cameron Aug 19, 2023
71d54cb
Merge branch 'main' into RFC30/test2
thomas-k-cameron Dec 9, 2023
d53c7c4
update
thomas-k-cameron Dec 9, 2023
c46ae34
modified: codegen-core/src/main/kotlin/software/amazon/smithy/rust/…
thomas-k-cameron Dec 9, 2023
ac6928d
update
thomas-k-cameron Dec 9, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,85 @@ import java.nio.file.Path
import kotlin.io.path.absolutePathString
import kotlin.io.path.writeText

// cargo commands and env values
private object Commands {
const val CargoFmt = "cargo fmt"
const val CargoClippy = "cargo clippy"

private const val cfgUnstable = "--cfg aws_sdk_unstable"
private const val allFeature = "--all-features"

// helper
private fun func(s: String, add: String, flag: Boolean): String = if (flag) { "$s $add" } else { s }

// unstable flag
fun cargoEnvDenyWarnings(enableUnstable: Boolean): Map<String, String> {
return mapOf(
"RUSTFLAGS" to func("-D warnings", cfgUnstable, enableUnstable),
)
}

fun cargoEnvAllowDeadCode(enableUnstable: Boolean): Map<String, String> {
return mapOf(
"RUSTFLAGS" to func("-A dead_code", cfgUnstable, enableUnstable),
)
}

// enable all features
// e.g.
// ```kotlin
// cargoTest(true)
// // cargo test --all-features
// cargoTest(false)
// // cargo test
// ```
fun cargoTest(enableAllFeatures: Boolean): String {
return func("cargo test", allFeature, enableAllFeatures)
}

// enable all features
// e.g.
// ```kotlin
// cargoCheck(true)
// // cargo test --all-features
// cargoCheck(false)
// // cargo test
// ```
fun cargoCheck(enableAllFeatures: Boolean): String {
return func("cargo check", allFeature, enableAllFeatures)
}

// enable features specified in the array
// e.g.
// ```kotlin
// cargoTest(["serde-serialize", "serde-deserialize"])
// // cargo test --features serde-serialize serde-deserialize
// ```
fun cargoTest(featuresToEnable: Array<String>?): String {
if (featuresToEnable != null) {
val s = featuresToEnable.joinToString { " " }
return "cargo test --features $s"
} else {
return "cargo test"
}
}

// enable features specified in the array
// e.g.
// ```kotlin
// cargoCheck(["serde-serialize", "serde-deserialize"])
// // cargo check --features serde-serialize serde-deserialize
// ```
fun cargoCheck(featuresToEnable: Array<String>?): String {
if (featuresToEnable != null) {
val s = featuresToEnable.joinToString { " " }
return "cargo check --features $s"
} else {
return "cargo check"
}
}
}

val TestModuleDocProvider = object : ModuleDocProvider {
override fun docsWriter(module: RustModule.LeafModule): Writable = writable {
docs("Some test documentation\n\nSome more details...")
Expand Down Expand Up @@ -319,10 +398,30 @@ fun FileManifest.printGeneratedFiles() {
* Setting `runClippy` to true can be helpful when debugging clippy failures, but
* should generally be set to `false` to avoid invalidating the Cargo cache between
* every unit test run.
* If you want to enable each features individually, specify the name of the feature on featuresToEnable.
* e.g.
* ```kotlin
* compileAndTest(featuresToEnable = ["this", "that"])
* ```
* All features are enabled by default. If you wish to disable them, set enableAllFeatures to False.
* ```kotlin
* compileAndTest(enableAllFeatures = false)
* ```
*
* You can run with `--cfg aws_sdk_unstable` by setting enableUnstableFlag to True.
* This feature is not enabled by default.
* e.g.
* ```kotlin
* compileAndTest(enableUnstableFlag = true)
* compileAndTest(enableUnstableFlag = true, featuresToEnable = ["serde-serialize", "serde-deserialize"])
* ```
*/
fun TestWriterDelegator.compileAndTest(
runClippy: Boolean = false,
expectFailure: Boolean = false,
enableUnstableFlag: Boolean = false,
enableAllFeatures: Boolean = true,
featuresToEnable: Array<String>? = null,
): String {
val stubModel = """
namespace fake
Expand All @@ -339,21 +438,29 @@ 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
}

// Clean `RUSTFLAGS` because in CI we pass in `--deny warnings` and
// we still generate test code with warnings.
// TODO(https://github.com/smithy-lang/smithy-rs/issues/3194)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I remove this comment?

I added a function that create a map which cleans up rustflag as well.

val env = mapOf("RUSTFLAGS" to "")
// val env = mapOf("RUSTFLAGS" to "")
//
val env = Commands.cargoEnvAllowDeadCode(enableUnstableFlag)
baseDir.writeDotCargoConfigToml(listOf("--allow", "dead_code"))

val testOutput = "cargo test".runCommand(baseDir, env)
var testCommand = Commands.cargoTest(enableUnstableFlag)
if (featuresToEnable != null) {
testCommand = Commands.cargoCheck(featuresToEnable)
}

val testOutput = testCommand.runCommand(baseDir, env)
if (runClippy) {
"cargo clippy --all-features".runCommand(baseDir, env)
Commands.CargoClippy.runCommand(baseDir, env)
}

return testOutput
}

Expand Down Expand Up @@ -392,6 +499,7 @@ fun RustWriter.compileAndTest(
main: String = "",
clippy: Boolean = false,
expectFailure: Boolean = false,
enableUnstable: Boolean = false,
): String {
val deps = this.dependencies
.map { RustDependency.fromSymbolDependency(it) }
Expand All @@ -410,9 +518,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(enableUnstable).runCommand(tempDir.toPath())
} else {
"cargo check".runCommand(tempDir.toPath())
Commands.cargoCheck(enableUnstable).runCommand(tempDir.toPath())
}
if (expectFailure) {
println("Test sources for debugging: file://${testModule.absolutePath}")
Expand Down Expand Up @@ -521,4 +629,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, enableUnstableFlag: Boolean = true) = this.runCommand(crate, Commands.cargoEnvDenyWarnings(enableUnstableFlag))
Loading