|  | 
|  | 1 | +extern crate tempfile; | 
|  | 2 | + | 
|  | 3 | +use std::{env, error::Error, fs, path::Path}; | 
|  | 4 | + | 
|  | 5 | +#[test] | 
|  | 6 | +fn handling_reset_cache_after_set_var() { | 
|  | 7 | +    let tmp_dir = tempfile::tempdir().unwrap(); | 
|  | 8 | + | 
|  | 9 | +    fill_dir(tmp_dir.path()).unwrap(); | 
|  | 10 | + | 
|  | 11 | +    if cfg!(all( | 
|  | 12 | +        target_os = "linux", | 
|  | 13 | +        target_arch = "x86_64", | 
|  | 14 | +        target_vendor = "unknown" | 
|  | 15 | +    )) { | 
|  | 16 | +        env::set_var("TARGET", "x86_64-unknown-linux-gnu"); | 
|  | 17 | +        env::set_var("HOST", "x86_64-unknown-linux-gnu"); | 
|  | 18 | +        env::set_var("OUT_DIR", tmp_dir.path()); | 
|  | 19 | +        env::set_var("PROFILE", "debug"); | 
|  | 20 | +        env::set_var("OPT_LEVEL", "0"); | 
|  | 21 | +        env::set_var("DEBUG", "true"); | 
|  | 22 | +        let dst = cmake::Config::new(tmp_dir.path()) | 
|  | 23 | +            .define("OPT1", "False") | 
|  | 24 | +            .build_target("all") | 
|  | 25 | +            .build() | 
|  | 26 | +            .join("build"); | 
|  | 27 | + | 
|  | 28 | +        assert!(fs::read_to_string(dst.join("CMakeCache.txt")) | 
|  | 29 | +            .unwrap() | 
|  | 30 | +            .contains("OPT1:BOOL=False")); | 
|  | 31 | +        env::set_var("CC", "clang"); | 
|  | 32 | +        env::set_var("CXX", "clang++"); | 
|  | 33 | +        let dst = cmake::Config::new(tmp_dir.path()) | 
|  | 34 | +            .define("OPT1", "False") | 
|  | 35 | +            .build_target("all") | 
|  | 36 | +            .build() | 
|  | 37 | +            .join("build"); | 
|  | 38 | + | 
|  | 39 | +        assert!(fs::read_to_string(dst.join("CMakeCache.txt")) | 
|  | 40 | +            .unwrap() | 
|  | 41 | +            .contains("OPT1:BOOL=False")); | 
|  | 42 | +    } | 
|  | 43 | +} | 
|  | 44 | + | 
|  | 45 | +fn fill_dir(tmp_dir: &Path) -> Result<(), Box<dyn Error>> { | 
|  | 46 | +    fs::write( | 
|  | 47 | +        tmp_dir.join("CMakeLists.txt"), | 
|  | 48 | +        r#" | 
|  | 49 | +project(xyz) | 
|  | 50 | +cmake_minimum_required(VERSION 3.9) | 
|  | 51 | +
 | 
|  | 52 | +option(OPT1 "some option" ON) | 
|  | 53 | +add_executable(xyz main.cpp) | 
|  | 54 | +"#, | 
|  | 55 | +    )?; | 
|  | 56 | +    fs::write( | 
|  | 57 | +        tmp_dir.join("main.cpp"), | 
|  | 58 | +        r#" | 
|  | 59 | +#include <cstdio> | 
|  | 60 | +#define DO_STRINGIFY(x) #x | 
|  | 61 | +#define STRINGIFY(x) DO_STRINGIFY(x) | 
|  | 62 | +int main() { | 
|  | 63 | +	printf("option: %s\n", STRINGIFY(OPT1)); | 
|  | 64 | +} | 
|  | 65 | +"#, | 
|  | 66 | +    )?; | 
|  | 67 | +    Ok(()) | 
|  | 68 | +} | 
0 commit comments