@@ -40,9 +40,11 @@ import software.amazon.smithy.rust.codegen.core.util.letIf
4040import software.amazon.smithy.rust.codegen.core.util.orNullIfEmpty
4141import software.amazon.smithy.rust.codegen.core.util.runCommand
4242import java.io.File
43+ import java.io.FileInputStream
4344import java.nio.file.Files
4445import java.nio.file.Files.createTempDirectory
4546import java.nio.file.Path
47+ import java.util.Properties
4648import kotlin.io.path.absolutePathString
4749import kotlin.io.path.writeText
4850
@@ -54,6 +56,8 @@ val TestModuleDocProvider =
5456 }
5557 }
5658
59+ val projectRootDir by lazy { File (" git rev-parse --show-toplevel" .runCommand().replace(" \n " , " " )) }
60+
5761/* *
5862 * Waiting for Kotlin to stabilize their temp directory functionality
5963 */
@@ -65,6 +69,38 @@ private fun tempDir(directory: File? = null): File {
6569 }
6670}
6771
72+ /* *
73+ * This function returns the minimum supported Rust version, as specified in the `gradle.properties` file
74+ * located at the root of the project.
75+ */
76+ fun msrv (): String {
77+ val properties = Properties ()
78+ val propertiesFilePath = projectRootDir.resolve(" gradle.properties" )
79+
80+ FileInputStream (propertiesFilePath).use { inputStream ->
81+ properties.load(inputStream)
82+ }
83+
84+ return properties.getProperty(" rust.msrv" )
85+ }
86+
87+ /* *
88+ * Generates the `rust-toolchain.toml` file in the specified directory.
89+ *
90+ * The compiler version is set in `gradle.properties` under the `rust.msrv` property.
91+ * The Gradle task `GenerateMsrvTask` generates the Kotlin class
92+ * `software.amazon.smithy.rust.codegen.core.Msrv` and writes the value of `rust.msrv` into it.
93+ */
94+ private fun File.generateRustToolchainToml () {
95+ resolve(" rust-toolchain.toml" ).writeText(
96+ // Help rust select the right version when we run cargo test.
97+ """
98+ [toolchain]
99+ channel = "${msrv()} "
100+ """ .trimIndent(),
101+ )
102+ }
103+
68104/* *
69105 * Creates a Cargo workspace shared among all tests
70106 *
@@ -87,8 +123,7 @@ object TestWorkspace {
87123 private val subprojects = mutableListOf<String >()
88124
89125 private val cargoLock: File by lazy {
90- val projectDir = " git rev-parse --show-toplevel" .runCommand().replace(" \n " , " " )
91- File (projectDir).resolve(" aws/sdk/Cargo.lock" )
126+ projectRootDir.resolve(" aws/sdk/Cargo.lock" )
92127 }
93128
94129 init {
@@ -121,12 +156,7 @@ object TestWorkspace {
121156 version = "0.0.1"
122157 """ .trimIndent(),
123158 )
124- newProject.resolve(" rust-toolchain.toml" ).writeText(
125- // help rust select the right version when we run cargo test
126- // TODO(https://github.com/smithy-lang/smithy-rs/issues/2048): load this from the msrv property using a
127- // method as we do for runtime crate versions
128- " [toolchain]\n channel = \" 1.78.0\"\n " ,
129- )
159+ newProject.generateRustToolchainToml()
130160 // ensure there at least an empty lib.rs file to avoid broken crates
131161 newProject.resolve(" src" ).mkdirs()
132162 newProject.resolve(" src/lib.rs" ).writeText(" " )
@@ -181,7 +211,11 @@ fun generatePluginContext(
181211 runtimeConfig : RuntimeConfig ? = null,
182212 overrideTestDir : File ? = null,
183213): Pair <PluginContext , Path > {
184- val testDir = overrideTestDir ? : TestWorkspace .subproject()
214+ val testDir =
215+ overrideTestDir?.apply {
216+ mkdirs()
217+ generateRustToolchainToml()
218+ } ? : TestWorkspace .subproject()
185219 val moduleName = " test_${testDir.nameWithoutExtension} "
186220 val testPath = testDir.toPath()
187221 val manifest = FileManifest .create(testPath)
0 commit comments