#[cfg(test)]
mod tests {
    use std::path::Path;

    fn run_south_with_args(args: &[&str]) {
        let mut my_args: Vec<&str> = Vec::from(["run", "--bin", "south"]);
        my_args.extend_from_slice(&args);

        let output = std::process::Command::new("cargo")
            .args(my_args)
            .output()
            .expect("failed to execute process");

        if !output.status.success() {
            let stdout = String::from_utf8_lossy(&output.stdout);
            let stderr = String::from_utf8_lossy(&output.stderr);
            panic!(
                "south command failed:\nstdout: {}\nstderr: {}",
                stdout, stderr
            );
        }
    }

    #[test]
    fn test_workspace() {
        let workspace_dir = Path::new("./tests/test_workspace")
            .canonicalize()
            .unwrap();
        assert!(workspace_dir.exists());

        let project_dir = workspace_dir.join("test_project").canonicalize().unwrap();
        assert!(project_dir.exists());

        // Clean up south.yaml if leftover from previous run
        let south_yaml = project_dir.join("south.yaml");
        if south_yaml.exists() {
            std::fs::remove_file(&south_yaml).unwrap();
        }

        // Create south manifest with a unique project-dir to avoid config conflicts
        run_south_with_args(&[
            "new",
            "--project-dir",
            project_dir.as_os_str().to_str().unwrap(),
        ]);

        // Check that south.yaml exists and contains valid YAML
        assert!(south_yaml.exists());
        let contents = std::fs::read_to_string(&south_yaml).unwrap();
        assert!(contents.contains("repos"));

        // Clean up
        std::fs::remove_file(&south_yaml).unwrap();
    }
}
