Skip to content

Commit

Permalink
Add new tests for paths at top level
Browse files Browse the repository at this point in the history
This supplements the tests in Fs, as we can now deal with concepts
like working directory in a much easier way.
  • Loading branch information
jonathanrainer committed Jul 19, 2024
1 parent d2336d1 commit 6c2b0e5
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
7 changes: 4 additions & 3 deletions tests/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use assert_cmd::Command;
use predicates::prelude::*;

mod config;
mod dev;
mod output;
mod schema;

use assert_cmd::Command;
use predicates::prelude::*;

#[test]
fn its_executable() {
let mut cmd = Command::cargo_bin("rover").unwrap();
Expand Down
1 change: 1 addition & 0 deletions tests/output/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod to_file;
70 changes: 70 additions & 0 deletions tests/output/to_file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use std::fs;

use assert_cmd::Command;
use camino::Utf8PathBuf;
use rstest::rstest;
use speculoos::assert_that;
use speculoos::prelude::BooleanAssertions;
use tempfile::TempDir;

use houston::{Config, Profile};
use rover::utils::env::RoverEnvKey;

const CUSTOM_PROFILE: &str = "custom-profile";
const CUSTOM_API_KEY: &str = "custom-api-key";

#[rstest]
#[case::simple_file_path("profiles.json", vec![], vec![], Utf8PathBuf::from("profiles.json"), false)]
#[case::simple_file_path_local("./profiles.json", vec![], vec![], Utf8PathBuf::from("profiles.json"), false)]
#[case::file_path_with_prefix("a/b/c/profiles.json", vec![Utf8PathBuf::from("a/b/c")], vec![], Utf8PathBuf::from("a/b/c/profiles.json"), false)]
#[case::file_path_with_prefix_failure_due_to_intermediate_files("a/b/c/profiles.json", vec![Utf8PathBuf::from("a")], vec![Utf8PathBuf::from("a/b")], Utf8PathBuf::from("a/b/c/profiles.json"), true)]
#[case::file_path_with_prefix_missing_inermediate("a/b/c/profiles.json", vec![Utf8PathBuf::from("a/b")], vec![], Utf8PathBuf::from("a/b/c/profiles.json"), false)]
#[case::complex_path("a/b/../../a/b/c/profiles.json", vec![Utf8PathBuf::from("a/b/c")], vec![], Utf8PathBuf::from("a/b/c/profiles.json"), false)]
#[case::complex_path_failure_due_to_intermediate_files("a/b/../../a/b/c/profiles.json", vec![Utf8PathBuf::from("a")], vec![Utf8PathBuf::from("a/b")], Utf8PathBuf::from("a/b/c/profiles.json"), true)]
#[case::complex_path_missing_intermediates("a/b/../../a/b/c/profiles.json", vec![Utf8PathBuf::from("a/b")], vec![], Utf8PathBuf::from("a/b/c/profiles.json"), false)]
fn it_can_write_files_correctly_no_matter_the_input_path(
#[case] output_argument: &str,
#[case] existing_paths: Vec<Utf8PathBuf>,
#[case] existing_files: Vec<Utf8PathBuf>,
#[case] expected_output_path: Utf8PathBuf,
#[case] should_fail: bool,
) {
let temp_config_dir = TempDir::new().expect("Could not create temporary directory");
let temp_config_dir_path = Utf8PathBuf::from_path_buf(temp_config_dir.into_path()).unwrap();
let config = Config::new(Some(&temp_config_dir_path).as_ref(), None).unwrap();
Profile::set_api_key(CUSTOM_PROFILE, &config, CUSTOM_API_KEY).unwrap();

let temp_output_dir = TempDir::new().expect("Could not create temporary directory");
let temp_output_dir_path = Utf8PathBuf::from_path_buf(temp_output_dir.into_path()).unwrap();
// Create any directories we want to exist in advance
for existing_path in existing_paths {
let path_to_create = temp_output_dir_path.join(existing_path);
fs::create_dir_all(path_to_create).expect("Could not create existing directories");
}
// Create any files we want to exist in advance
for existing_file in existing_files {
let file = temp_output_dir_path.join(existing_file);
fs::write(file, "foo bar bash").expect("Could not create existing directories");
}

let mut starter_cmd = Command::cargo_bin("rover").unwrap();
starter_cmd
.env(RoverEnvKey::ConfigHome.to_string(), temp_config_dir_path)
.args(vec![
"config",
"list",
"--format",
"json",
"--output",
output_argument,
])
.current_dir(&temp_output_dir_path)
.assert()
.success();

if should_fail {
assert_that(&temp_output_dir_path.join(expected_output_path).exists()).is_false()
} else {
assert_that(&temp_output_dir_path.join(expected_output_path).exists()).is_true()
}
}

0 comments on commit 6c2b0e5

Please sign in to comment.