Skip to content

Commit

Permalink
CARGO: properly extract build target if there isn't cargo config file
Browse files Browse the repository at this point in the history
  • Loading branch information
Undin authored and yopox committed Feb 27, 2023
1 parent bb28d83 commit b1faae8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
3 changes: 2 additions & 1 deletion src/main/kotlin/org/rust/cargo/toolchain/tools/Cargo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,8 @@ class Cargo(
return Err(RsDeserializationException(e))
}

val buildTarget = tree.at("/build/target").asText()
val buildTargetNode = tree.at("/build/target")
val buildTarget = if (buildTargetNode.isMissingNode) null else buildTargetNode.asText()
val env = tree.at("/env").fields().asSequence().toList().mapNotNull { field ->
// Value can be either string or object with additional `forced` and `relative` params.
// https://doc.rust-lang.org/cargo/reference/config.html#env
Expand Down
43 changes: 28 additions & 15 deletions src/test/kotlin/org/rust/cargo/toolchain/CargoConfigTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,57 @@

package org.rust.cargo.toolchain

import org.intellij.lang.annotations.Language
import org.rust.MinRustcVersion
import org.rust.cargo.CargoConfig
import org.rust.cargo.RsWithToolchainTestBase
import org.rust.cargo.project.model.cargoProjects
import org.rust.fileTree
import org.rust.singleWorkspace

@MinRustcVersion("1.53.0")
class CargoConfigTest : RsWithToolchainTestBase() {

fun `test default config`() {
buildProject {
file("Cargo.toml", CARGO_TOML)
dir("src") { file("main.rs") }
}

val cargoConfig = project.cargoProjects.singleWorkspace().cargoConfig

assertEquals(CargoConfig.DEFAULT, cargoConfig)
}

fun `test build target`() {
fileTree {
buildProject {
dir(".cargo") {
file("config.toml", """
[build]
target = "wasm32-unknown-unknown"
""")
toml("config.toml", """
[build]
target = "wasm32-unknown-unknown"
""")
}
file("Cargo.toml", CARGO_TOML)
dir("src") { file("main.rs") }
}.create()
}

val buildTarget = project.cargoProjects.singleWorkspace().cargoConfig.buildTarget

assertEquals("wasm32-unknown-unknown", buildTarget)
}

fun `test env`() {
fileTree {
buildProject {
dir(".cargo") {
file("config.toml", """
[env]
foo = "42"
bar = { value = "24", forced = true }
baz = { value = "hello/world", relative = true }
""")
toml("config.toml", """
[env]
foo = "42"
bar = { value = "24", forced = true }
baz = { value = "hello/world", relative = true }
""")
}
file("Cargo.toml", CARGO_TOML)
dir("src") { file("main.rs") }
}.create()
}

val env = project.cargoProjects.singleWorkspace().cargoConfig.env

Expand All @@ -53,6 +65,7 @@ class CargoConfigTest : RsWithToolchainTestBase() {
}

companion object {
@Language("TOML")
private val CARGO_TOML = """
[package]
name = "foo"
Expand Down

0 comments on commit b1faae8

Please sign in to comment.