test-mumu 0.1.10

Test suite plugin for the Lava language
Documentation
use std::env;
use std::fs;
use std::io::ErrorKind;
use std::path::PathBuf;

fn main() {
    let out_dir = env::var("OUT_DIR").expect("No OUT_DIR env var");
    let mut artifacts_dir = PathBuf::from(&out_dir);

    // Pop twice to back up to target/(debug|release)
    artifacts_dir.pop();
    artifacts_dir.pop();

    #[cfg(windows)]
    let lib_filename = "mumutest.dll";
    #[cfg(target_os = "macos")]
    let lib_filename = "libmumutest.dylib";
    #[cfg(all(not(windows), not(target_os = "macos")))]
    let lib_filename = "libmumutest.so";

    // We'll create/copy/symlink a file so that `extend("test")` remains valid:
    let link_filename = lib_filename.replace("mumutest", "test");

    let lib_path = artifacts_dir.join(&lib_filename);
    let link_path = artifacts_dir.join(&link_filename);

    // Remove old leftover file if any
    let _ = fs::remove_file(&link_path);

    #[cfg(unix)]
    {
        match std::os::unix::fs::symlink(&lib_path, &link_path) {
            Ok(_) => {
                println!(
                    "cargo:warning=Created symlink {:?} -> {:?}",
                    link_filename, lib_filename
                );
            }
            Err(e) => {
                if e.kind() == ErrorKind::Unsupported {
                    println!("cargo:warning=Symlink unsupported => copying instead.");
                    fs::copy(&lib_path, &link_path)
                        .expect("Failed to copy plugin library => rename to test");
                } else {
                    panic!("Failed to symlink for test plugin: {:?}", e);
                }
            }
        }
    }

    #[cfg(not(unix))]
    {
        println!("cargo:warning=On this platform, copying instead of symlink for test plugin.");
        fs::copy(&lib_path, &link_path)
            .expect("Failed to copy plugin library => rename to test");
    }
}