Skip to content

Commit 9b95094

Browse files
committed
sanitise cache checkout dir and sanity test it
1 parent c5c88d0 commit 9b95094

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

.github/workflows/push.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ jobs:
2424
- uses: moonrepo/setup-rust@v1
2525
- run: rustup default stable
2626
- run: rustup update
27+
- run: cargo test
2728
- run: cargo install --path crates/cargo-gpu
2829
- run: cargo gpu install
2930
- run: cargo gpu build --shader-crate crates/shader-crate-template --output-dir test-shaders

crates/cargo-gpu/src/main.rs

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,19 +135,34 @@ struct Spirv {
135135
channel: String,
136136
}
137137

138+
impl Default for Spirv {
139+
fn default() -> Self {
140+
Self {
141+
dep: Self::DEFAULT_DEP.into(),
142+
channel: Self::DEFAULT_CHANNEL.into(),
143+
}
144+
}
145+
}
146+
138147
impl core::fmt::Display for Spirv {
139148
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
140149
format!("{}+{}", self.dep, self.channel).fmt(f)
141150
}
142151
}
143152

144153
impl Spirv {
154+
const DEFAULT_DEP: &str = r#"{ git = "https://github.com/Rust-GPU/rust-gpu.git" }"#;
155+
const DEFAULT_CHANNEL: &str = "nightly-2024-04-24";
156+
145157
/// Returns a string suitable to use as a directory.
146158
///
147159
/// Created from the spirv-builder source dep and the rustc channel.
148160
fn to_dirname(&self) -> String {
149161
self.to_string()
150-
.replace([std::path::MAIN_SEPARATOR, '.', ':', '@', '='], "_")
162+
.replace(
163+
[std::path::MAIN_SEPARATOR, '\\', '/', '.', ':', '@', '='],
164+
"_",
165+
)
151166
.split(['{', '}', ' ', '\n', '"', '\''])
152167
.collect::<Vec<_>>()
153168
.concat()
@@ -258,16 +273,13 @@ fn target_spec_dir() -> std::path::PathBuf {
258273
#[derive(Parser, Debug)]
259274
struct Install {
260275
/// spirv-builder dependency, written just like in a Cargo.toml file.
261-
#[clap(
262-
long,
263-
default_value = r#"{ git = "https://github.com/Rust-GPU/rust-gpu.git" }"#
264-
)]
276+
#[clap(long, default_value = Spirv::DEFAULT_DEP)]
265277
spirv_builder: String,
266278

267279
/// Rust toolchain channel to use to build `spirv-builder`.
268280
///
269281
/// This must match the `spirv_builder` argument.
270-
#[clap(long, default_value = "nightly-2024-04-24")]
282+
#[clap(long, default_value = Spirv::DEFAULT_CHANNEL)]
271283
rust_toolchain: String,
272284

273285
/// Force `spirv-builder-cli` and `rustc_codegen_spirv` to be rebuilt.
@@ -312,6 +324,7 @@ impl Install {
312324
fn run(&self) -> (std::path::PathBuf, std::path::PathBuf) {
313325
// Ensure the cache dir exists
314326
let cache_dir = cache_dir();
327+
log::info!("cache directory is '{}'", cache_dir.display());
315328
std::fs::create_dir_all(&cache_dir).unwrap_or_else(|e| {
316329
log::error!(
317330
"could not create cache directory '{}': {e}",
@@ -757,7 +770,7 @@ fn dump_full_usage_for_readme() {
757770
println!("{}", buffer);
758771
}
759772

760-
fn write_help(buffer: &mut impl std::io::Write, cmd: &mut clap::Command, depth: usize) {
773+
fn write_help(buffer: &mut impl std::io::Write, cmd: &mut clap::Command, _depth: usize) {
761774
if cmd.get_name() == "help" {
762775
return;
763776
}
@@ -774,14 +787,30 @@ fn write_help(buffer: &mut impl std::io::Write, cmd: &mut clap::Command, depth:
774787

775788
for sub in cmd.get_subcommands_mut() {
776789
let _ = writeln!(buffer);
777-
write_help(buffer, sub, depth + 1);
790+
write_help(buffer, sub, _depth + 1);
778791
}
779792
}
780793

781794
#[cfg(test)]
782795
mod test {
783796
use super::*;
784797

798+
#[test]
799+
fn cached_checkout_dir_sanity() {
800+
let spirv = Spirv::default();
801+
let dir = spirv.cached_checkout_path();
802+
let name = dir
803+
.file_name()
804+
.unwrap()
805+
.to_str()
806+
.map(|s| s.to_string())
807+
.unwrap();
808+
assert_eq!(
809+
"git_https___github_com_Rust-GPU_rust-gpu_git+nightly-2024-04-24",
810+
&name
811+
);
812+
}
813+
785814
#[test]
786815
fn builder_from_params() {
787816
let shader_crate = std::path::PathBuf::from("../shader-crate-template");

0 commit comments

Comments
 (0)