|
| 1 | +// Copyright 2022 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 4 | +// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 5 | +// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your |
| 6 | +// option. This file may not be copied, modified, or distributed |
| 7 | +// except according to those terms. |
| 8 | + |
| 9 | +use std::env; |
| 10 | +use std::process::{exit, Command}; |
| 11 | + |
| 12 | +const FEAT_OPTIONS: [bool; 2] = [false, true]; |
| 13 | +const FEAT_BYTEMUCK: &str = "bytemuck"; |
| 14 | +const FEAT_SERDE: &str = "serde"; |
| 15 | +const FEAT_STD: &str = "std"; |
| 16 | + |
| 17 | +fn run_cmd(mut cmd: Command) { |
| 18 | + println!("Running: {}", format!("{:?}", cmd).replace('"', "")); |
| 19 | + let status = cmd.status().expect("failed to launch"); |
| 20 | + if !status.success() { |
| 21 | + panic!("command failed: {}", status); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +#[derive(Clone, Copy)] |
| 26 | +enum CargoAction { |
| 27 | + Test, |
| 28 | + Lint, |
| 29 | +} |
| 30 | + |
| 31 | +impl CargoAction { |
| 32 | + fn as_str(self) -> &'static str { |
| 33 | + match self { |
| 34 | + Self::Lint => "clippy", |
| 35 | + Self::Test => "test", |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +fn get_cargo_cmd( |
| 41 | + action: CargoAction, |
| 42 | + package: &str, |
| 43 | + features: &[&str], |
| 44 | +) -> Command { |
| 45 | + let mut cmd = Command::new("cargo"); |
| 46 | + cmd.args(&[action.as_str(), "--package", package]); |
| 47 | + if !features.is_empty() { |
| 48 | + cmd.args(&["--features", &features.join(",")]); |
| 49 | + } |
| 50 | + match action { |
| 51 | + CargoAction::Test => {} |
| 52 | + CargoAction::Lint => { |
| 53 | + cmd.args(&["--", "-D", "warnings"]); |
| 54 | + } |
| 55 | + } |
| 56 | + cmd |
| 57 | +} |
| 58 | + |
| 59 | +fn test_package(package: &str, features: &[&str]) { |
| 60 | + run_cmd(get_cargo_cmd(CargoAction::Lint, package, features)); |
| 61 | + run_cmd(get_cargo_cmd(CargoAction::Test, package, features)); |
| 62 | +} |
| 63 | + |
| 64 | +fn test_uguid() { |
| 65 | + for feat_bytemuck in FEAT_OPTIONS { |
| 66 | + for feat_serde in FEAT_OPTIONS { |
| 67 | + for feat_std in FEAT_OPTIONS { |
| 68 | + let mut features = Vec::new(); |
| 69 | + if feat_bytemuck { |
| 70 | + features.push(FEAT_BYTEMUCK); |
| 71 | + } |
| 72 | + if feat_serde { |
| 73 | + features.push(FEAT_SERDE); |
| 74 | + } |
| 75 | + if feat_std { |
| 76 | + features.push(FEAT_STD); |
| 77 | + } |
| 78 | + |
| 79 | + test_package("uguid", &features); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +fn test_gpt_disk_types() { |
| 86 | + for feat_bytemuck in FEAT_OPTIONS { |
| 87 | + for feat_std in FEAT_OPTIONS { |
| 88 | + let mut features = Vec::new(); |
| 89 | + if feat_bytemuck { |
| 90 | + features.push(FEAT_BYTEMUCK); |
| 91 | + } |
| 92 | + if feat_std { |
| 93 | + features.push(FEAT_STD); |
| 94 | + } |
| 95 | + |
| 96 | + test_package("gpt_disk_types", &features); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +fn test_gpt_disk_io() { |
| 102 | + for feat_std in FEAT_OPTIONS { |
| 103 | + let mut features = Vec::new(); |
| 104 | + if feat_std { |
| 105 | + features.push("std"); |
| 106 | + } |
| 107 | + |
| 108 | + test_package("gpt_disk_types", &features); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +fn main() { |
| 113 | + let args: Vec<_> = env::args().collect(); |
| 114 | + let arg_test_all = "test_all"; |
| 115 | + let arg_test_uguid = "test_uguid"; |
| 116 | + let arg_test_gpt_disk_types = "test_gpt_disk_types"; |
| 117 | + let arg_test_gpt_disk_io = "test_gpt_disk_io"; |
| 118 | + let actions = &[ |
| 119 | + arg_test_all, |
| 120 | + arg_test_uguid, |
| 121 | + arg_test_gpt_disk_types, |
| 122 | + arg_test_gpt_disk_io, |
| 123 | + ]; |
| 124 | + if args.len() != 2 || !actions.contains(&args[1].as_ref()) { |
| 125 | + println!("usage: cargo xtask [{}]", actions.join("|")); |
| 126 | + exit(1); |
| 127 | + } |
| 128 | + |
| 129 | + let action = &args[1]; |
| 130 | + if action == arg_test_all || action == arg_test_uguid { |
| 131 | + test_uguid(); |
| 132 | + } |
| 133 | + if action == arg_test_all || action == arg_test_gpt_disk_types { |
| 134 | + test_gpt_disk_types(); |
| 135 | + } |
| 136 | + if action == arg_test_all || action == arg_test_gpt_disk_io { |
| 137 | + test_gpt_disk_io(); |
| 138 | + } |
| 139 | +} |
0 commit comments