use assert_cmd::prelude::*;
use assert_fs::prelude::*;
use dirs;
use std::process::Command;
#[test]
fn cli_gen_creates_files() {
let td = assert_fs::TempDir::new().unwrap();
let root = td.path();
let pack_dir = root.join("pack");
let templates_dir = pack_dir.join("templates/cli/subcommand");
std::fs::create_dir_all(&templates_dir).unwrap();
std::fs::write(
templates_dir.join("rust.tmpl"),
r#"---
to: out/hello.rs
vars: { cmd: hello }
---
pub fn hello() {}
"#,
)
.unwrap();
let lockfile_content = r#"version = "1.0.0"
generated = "2024-01-01T00:00:00Z"
[[packs]]
id = "local.test"
version = "1.0.0"
sha256 = "abc123"
source = "local"
"#;
std::fs::write(root.join("rgen.lock"), lockfile_content).unwrap();
let system_cache_dir = dirs::cache_dir().unwrap().join("rgen/rpacks");
let cache_dir = system_cache_dir.join("local.test/1.0.0");
let cache_templates_dir = cache_dir.join("templates");
std::fs::create_dir_all(&cache_templates_dir).unwrap();
let cache_template_dir = cache_templates_dir.join("cli/subcommand");
std::fs::create_dir_all(&cache_template_dir).unwrap();
std::fs::copy(
templates_dir.join("rust.tmpl"),
cache_template_dir.join("rust.tmpl"),
)
.unwrap();
std::fs::write(
cache_templates_dir.join("rgen.toml"),
r#"
[rpack]
id = "local.test"
name = "Local Test Pack"
version = "1.0.0"
description = "Local test pack"
license = "MIT"
rgen_compat = "1.0.0"
"#,
)
.unwrap();
let mut cmd = Command::cargo_bin("rgen").unwrap();
cmd.current_dir(root)
.arg("gen")
.arg("local.test:cli/subcommand/rust.tmpl")
.arg("--var")
.arg("cmd=hello")
.arg("--dry");
cmd.assert().success();
assert!(!root.join("out/hello.rs").exists());
}