Commit 028c5f81 authored by Wohlgemuth, Jason's avatar Wohlgemuth, Jason
Browse files

feat: Use which-rs crate for Command::test and update associated test to pass on windows

parent 1477a71c
Loading
Loading
Loading
Loading
Loading
+20 −1
Original line number Diff line number Diff line
@@ -578,7 +578,7 @@ dependencies = [
 "nix 0.24.3",
 "terminfo",
 "thiserror",
 "which",
 "which 4.4.2",
 "winapi 0.3.9",
]

@@ -1907,6 +1907,7 @@ dependencies = [
 "uriparse",
 "valuable",
 "valuable-derive",
 "which 6.0.3",
]

[[package]]
@@ -2757,6 +2758,18 @@ dependencies = [
 "rustix 0.38.31",
]

[[package]]
name = "which"
version = "6.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4ee928febd44d98f2f459a4a79bd4d928591333a494a10a868418ac1b39cf1f"
dependencies = [
 "either",
 "home",
 "rustix 0.38.31",
 "winsafe",
]

[[package]]
name = "winapi"
version = "0.2.8"
@@ -2969,6 +2982,12 @@ dependencies = [
 "memchr",
]

[[package]]
name = "winsafe"
version = "0.0.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d135d17ab770252ad95e9a872d365cf3090e3be864a34ab46f48555993efc904"

[[package]]
name = "ws2_32-sys"
version = "0.2.1"
+369 −371
Original line number Diff line number Diff line
@@ -370,62 +370,62 @@ LH:0
end_of_record
TN:
SF:/root/dev/command/pipe-lib/src/lib.rs
FN:387,Command::test
FN:414,Config::read
FN:438,Config::read_json
FN:459,Config::read_yaml
FN:476,Config::set_environment_variables
FN:490,<impl PartialEq for Details>::eq
FN:497,EnvironmentValue::from_string
FN:500,EnvironmentValue::from_value
FN:508,EnvironmentValue::prepend_prefix
FN:512,EnvironmentValue::trim_prefix
FN:516,EnvironmentValue::get_from_config
FN:523,EnvironmentValue::get_from_serde
FN:537,EnvironmentValue::get_all
FN:547,EnvironmentValue::print_all
FN:552,EnvironmentValue::set_variable
FN:562,EnvironmentValue::set_variables
FN:569,Extension::from_string
FN:578,Label::dry_run
FN:582,Label::invalid
FN:586,Label::fail
FN:590,Label::found
FN:594,Label::not_found
FN:598,Label::output
FN:602,Label::read
FN:606,Label::rejected
FN:610,Label::run
FN:614,Label::using
FN:620,ModuleUri<'_>::from
FN:630,ModuleUri<'_>::hash
FN:654,ModuleUri<'_>::get_whitelist
FN:657,ModuleUri<'_>::is_allowed
FN:660,ModuleUri<'_>::test
FN:661,ModuleUri<'_>::test::check
FN:678,ModuleUri<'_>::working_directory
FN:690,SemanticVersion::from_string
FN:713,Script::expand_arguments
FN:748,Script::expand_arguments_from
FN:783,Script::from_module
FN:841,Script::from_path
FN:864,Script::from_template
FN:881,Script::get_command
FN:887,Script::get_arguments
FN:898,Script::maybe_create_conda_environment
FN:931,Script::maybe_git_clone
FN:938,Script::python_default
FN:952,Script::run
FN:1022,Script::with_current_dir
FN:1060,Script::with_environment
FN:1065,Script::with_envs
FN:1074,StringTemplate::new
FN:1083,StringTemplate::expand
FN:1090,StringTemplate::get_variable
FN:1120,StringTemplate::is_handlebars
FN:1155,StringTemplate::remove_spaces
FN:1165,add_dot_slash
FN:1173,add_forward_slash
FN:388,Command::test
FN:411,Config::read
FN:435,Config::read_json
FN:456,Config::read_yaml
FN:473,Config::set_environment_variables
FN:487,<impl PartialEq for Details>::eq
FN:494,EnvironmentValue::from_string
FN:497,EnvironmentValue::from_value
FN:505,EnvironmentValue::prepend_prefix
FN:509,EnvironmentValue::trim_prefix
FN:513,EnvironmentValue::get_from_config
FN:520,EnvironmentValue::get_from_serde
FN:534,EnvironmentValue::get_all
FN:544,EnvironmentValue::print_all
FN:549,EnvironmentValue::set_variable
FN:559,EnvironmentValue::set_variables
FN:566,Extension::from_string
FN:575,Label::dry_run
FN:579,Label::invalid
FN:583,Label::fail
FN:587,Label::found
FN:591,Label::not_found
FN:595,Label::output
FN:599,Label::read
FN:603,Label::rejected
FN:607,Label::run
FN:611,Label::using
FN:617,ModuleUri<'_>::from
FN:627,ModuleUri<'_>::hash
FN:651,ModuleUri<'_>::get_whitelist
FN:654,ModuleUri<'_>::is_allowed
FN:657,ModuleUri<'_>::test
FN:658,ModuleUri<'_>::test::check
FN:675,ModuleUri<'_>::working_directory
FN:687,SemanticVersion::from_string
FN:710,Script::expand_arguments
FN:745,Script::expand_arguments_from
FN:780,Script::from_module
FN:838,Script::from_path
FN:861,Script::from_template
FN:878,Script::get_command
FN:884,Script::get_arguments
FN:895,Script::maybe_create_conda_environment
FN:928,Script::maybe_git_clone
FN:935,Script::python_default
FN:949,Script::run
FN:1019,Script::with_current_dir
FN:1057,Script::with_environment
FN:1062,Script::with_envs
FN:1071,StringTemplate::new
FN:1080,StringTemplate::expand
FN:1087,StringTemplate::get_variable
FN:1117,StringTemplate::is_handlebars
FN:1152,StringTemplate::remove_spaces
FN:1162,add_dot_slash
FN:1170,add_forward_slash
FNF:56
FNDA:1,Command::test
FNDA:1,Config::read
@@ -454,7 +454,7 @@ FNDA:0,Label::read
FNDA:0,Label::rejected
FNDA:0,Label::run
FNDA:0,Label::using
FNDA:1,ModuleUri<'_>::from
FNDA:2,ModuleUri<'_>::from
FNDA:1,ModuleUri<'_>::hash
FNDA:0,ModuleUri<'_>::get_whitelist
FNDA:0,ModuleUri<'_>::is_allowed
@@ -483,428 +483,426 @@ FNDA:1,StringTemplate::is_handlebars
FNDA:1,StringTemplate::remove_spaces
FNDA:1,add_dot_slash
FNDA:1,add_forward_slash
DA:387,1
DA:388,1
DA:389,2
DA:390,1
DA:391,3
DA:392,2
DA:394,3
DA:395,1
DA:396,1
DA:398,2
DA:399,1
DA:400,1
DA:401,2
DA:403,3
DA:404,1
DA:407,3
DA:408,1
DA:398,0
DA:399,0
DA:404,3
DA:405,1
DA:411,1
DA:412,2
DA:413,2
DA:414,1
DA:415,2
DA:416,2
DA:417,1
DA:418,0
DA:420,2
DA:421,1
DA:422,0
DA:426,2
DA:427,1
DA:429,0
DA:430,0
DA:438,1
DA:439,2
DA:440,2
DA:442,0
DA:443,0
DA:446,2
DA:447,1
DA:448,2
DA:449,0
DA:451,3
DA:452,1
DA:459,1
DA:460,2
DA:461,1
DA:463,0
DA:464,0
DA:467,2
DA:468,1
DA:469,2
DA:470,0
DA:472,3
DA:473,1
DA:415,0
DA:417,2
DA:418,1
DA:419,0
DA:423,2
DA:424,1
DA:426,0
DA:427,0
DA:435,1
DA:436,2
DA:437,2
DA:439,0
DA:440,0
DA:443,2
DA:444,2
DA:445,4
DA:446,0
DA:448,6
DA:449,2
DA:456,1
DA:457,2
DA:458,1
DA:460,0
DA:461,0
DA:464,2
DA:465,1
DA:466,2
DA:467,0
DA:469,3
DA:470,1
DA:473,0
DA:474,0
DA:475,0
DA:476,0
DA:477,0
DA:478,0
DA:479,0
DA:480,0
DA:481,0
DA:486,0
DA:490,1
DA:491,1
DA:483,0
DA:487,1
DA:488,1
DA:494,0
DA:495,0
DA:497,0
DA:498,0
DA:499,0
DA:500,0
DA:501,0
DA:502,0
DA:503,0
DA:504,0
DA:505,0
DA:508,0
DA:509,0
DA:512,1
DA:513,1
DA:514,2
DA:506,0
DA:509,1
DA:510,1
DA:511,2
DA:513,0
DA:514,0
DA:515,0
DA:516,0
DA:517,0
DA:518,0
DA:519,0
DA:520,0
DA:521,0
DA:522,0
DA:523,0
DA:524,0
DA:525,0
DA:526,0
DA:527,0
DA:528,0
DA:529,0
DA:530,0
DA:531,0
DA:533,0
DA:535,0
DA:537,1
DA:538,1
DA:539,3
DA:540,3
DA:541,3
DA:542,2
DA:543,1
DA:547,0
DA:548,0
DA:532,0
DA:534,1
DA:535,1
DA:536,2
DA:537,2
DA:538,2
DA:539,2
DA:540,1
DA:544,0
DA:545,0
DA:546,0
DA:549,0
DA:550,0
DA:551,0
DA:552,0
DA:553,0
DA:554,0
DA:555,0
DA:556,0
DA:557,0
DA:558,0
DA:559,0
DA:560,0
DA:562,0
DA:563,0
DA:565,0
DA:569,1
DA:570,2
DA:571,2
DA:572,3
DA:573,0
DA:578,0
DA:566,1
DA:567,2
DA:568,2
DA:569,3
DA:570,0
DA:575,0
DA:576,0
DA:577,0
DA:579,0
DA:580,0
DA:582,0
DA:581,0
DA:583,0
DA:584,0
DA:586,0
DA:585,0
DA:587,0
DA:588,0
DA:590,0
DA:589,0
DA:591,0
DA:592,0
DA:594,0
DA:595,0
DA:596,0
DA:598,1
DA:599,2
DA:600,1
DA:602,0
DA:593,0
DA:595,1
DA:596,3
DA:597,3
DA:599,0
DA:600,0
DA:601,0
DA:603,0
DA:604,0
DA:606,0
DA:605,0
DA:607,0
DA:608,0
DA:610,0
DA:609,0
DA:611,0
DA:612,0
DA:614,0
DA:615,0
DA:616,0
DA:620,1
DA:621,1
DA:622,2
DA:623,1
DA:624,2
DA:625,0
DA:613,0
DA:617,2
DA:618,2
DA:619,5
DA:620,2
DA:621,2
DA:622,0
DA:624,1
DA:627,1
DA:628,1
DA:629,1
DA:630,1
DA:631,1
DA:632,1
DA:633,1
DA:634,1
DA:635,0
DA:637,2
DA:641,2
DA:631,2
DA:632,0
DA:634,4
DA:638,4
DA:639,4
DA:641,4
DA:642,2
DA:644,2
DA:645,2
DA:647,1
DA:650,0
DA:647,0
DA:651,0
DA:652,0
DA:654,0
DA:655,0
DA:657,0
DA:658,0
DA:660,0
DA:659,0
DA:661,0
DA:662,0
DA:663,0
DA:664,0
DA:665,0
DA:666,0
DA:667,0
DA:668,0
DA:669,0
DA:670,0
DA:672,0
DA:674,0
DA:671,0
DA:675,0
DA:676,0
DA:677,0
DA:678,0
DA:679,0
DA:680,0
DA:681,0
DA:682,0
DA:684,0
DA:685,0
DA:690,1
DA:691,1
DA:692,3
DA:693,3
DA:694,3
DA:713,1
DA:687,1
DA:688,1
DA:689,3
DA:690,3
DA:691,3
DA:710,1
DA:711,2
DA:714,2
DA:717,2
DA:715,1
DA:716,2
DA:717,1
DA:718,1
DA:719,2
DA:720,1
DA:721,1
DA:723,2
DA:727,1
DA:728,1
DA:748,1
DA:749,3
DA:720,2
DA:724,1
DA:725,1
DA:745,1
DA:746,2
DA:749,2
DA:750,1
DA:751,2
DA:752,2
DA:753,2
DA:754,2
DA:755,2
DA:758,2
DA:760,1
DA:762,2
DA:766,1
DA:767,1
DA:757,1
DA:759,2
DA:763,1
DA:764,1
DA:780,0
DA:782,0
DA:783,0
DA:785,0
DA:786,0
DA:784,0
DA:787,0
DA:788,0
DA:789,0
DA:790,0
DA:791,0
DA:792,0
DA:793,0
DA:794,0
DA:796,0
DA:797,0
DA:795,0
DA:798,0
DA:799,0
DA:800,0
DA:801,0
DA:802,0
DA:803,0
DA:804,0
DA:807,0
DA:808,0
DA:805,0
DA:806,0
DA:809,0
DA:811,0
DA:812,0
DA:814,0
DA:815,0
DA:818,0
DA:824,0
DA:825,0
DA:841,1
DA:842,2
DA:843,1
DA:845,0
DA:846,0
DA:849,2
DA:850,1
DA:851,1
DA:864,1
DA:865,1
DA:866,3
DA:870,2
DA:872,2
DA:874,2
DA:877,1
DA:878,2
DA:879,3
DA:821,0
DA:822,0
DA:838,1
DA:839,2
DA:840,1
DA:842,0
DA:843,0
DA:846,2
DA:847,1
DA:848,1
DA:861,1
DA:862,1
DA:863,3
DA:867,2
DA:869,2
DA:871,2
DA:874,1
DA:875,2
DA:876,3
DA:878,1
DA:879,1
DA:880,1
DA:881,1
DA:882,1
DA:883,1
DA:884,1
DA:885,1
DA:886,1
DA:887,1
DA:888,1
DA:888,2
DA:889,1
DA:890,1
DA:891,2
DA:892,1
DA:894,2
DA:891,1
DA:895,0
DA:896,0
DA:897,0
DA:898,0
DA:899,0
DA:900,0
DA:901,0
DA:902,0
DA:903,0
DA:904,0
DA:905,0
DA:906,0
DA:907,0
DA:908,0
DA:909,0
DA:910,0
DA:911,0
DA:912,0
DA:913,0
DA:914,0
DA:915,0
DA:916,0
DA:918,0
DA:919,0
DA:922,0
DA:926,0
DA:927,0
DA:931,1
DA:932,2
DA:933,2
DA:934,2
DA:935,3
DA:936,1
DA:938,0
DA:939,0
DA:940,0
DA:948,0
DA:952,1
DA:953,1
DA:954,1
DA:955,3
DA:956,3
DA:957,4
DA:958,2
DA:959,12
DA:960,3
DA:923,0
DA:924,0
DA:928,1
DA:929,2
DA:930,2
DA:931,2
DA:932,3
DA:933,1
DA:935,0
DA:936,0
DA:937,0
DA:945,0
DA:949,1
DA:950,1
DA:951,1
DA:952,2
DA:953,2
DA:954,3
DA:955,1
DA:956,7
DA:957,2
DA:958,1
DA:959,1
DA:960,1
DA:961,1
DA:962,1
DA:963,1
DA:964,1
DA:969,3
DA:970,2
DA:972,0
DA:973,0
DA:976,1
DA:977,2
DA:978,3
DA:979,3
DA:982,3
DA:983,1
DA:985,0
DA:986,0
DA:989,2
DA:990,1
DA:991,3
DA:992,0
DA:993,0
DA:996,3
DA:997,2
DA:998,4
DA:966,2
DA:967,1
DA:969,0
DA:970,0
DA:973,1
DA:974,1
DA:975,2
DA:976,2
DA:979,2
DA:980,1
DA:982,0
DA:983,0
DA:986,1
DA:987,1
DA:988,2
DA:989,0
DA:990,0
DA:993,2
DA:994,1
DA:995,3
DA:996,1
DA:997,3
DA:998,3
DA:999,1
DA:1000,5
DA:1001,4
DA:1002,2
DA:1004,3
DA:1005,3
DA:1006,2
DA:1009,0
DA:1010,0
DA:1011,0
DA:1015,0
DA:1016,0
DA:1022,1
DA:1023,2
DA:1024,1
DA:1060,1
DA:1061,1
DA:1062,1
DA:1065,0
DA:1066,0
DA:1067,0
DA:1074,1
DA:1076,1
DA:1083,1
DA:1084,4
DA:1085,3
DA:1001,0
DA:1002,0
DA:1003,0
DA:1006,0
DA:1007,0
DA:1008,0
DA:1012,0
DA:1013,0
DA:1019,1
DA:1020,2
DA:1021,1
DA:1057,1
DA:1058,1
DA:1059,1
DA:1062,0
DA:1063,0
DA:1064,0
DA:1071,1
DA:1073,1
DA:1080,1
DA:1081,4
DA:1082,3
DA:1087,1
DA:1088,2
DA:1089,2
DA:1090,1
DA:1091,2
DA:1092,2
DA:1093,1
DA:1094,2
DA:1096,0
DA:1093,0
DA:1117,1
DA:1118,2
DA:1119,2
DA:1120,1
DA:1121,2
DA:1122,2
DA:1123,1
DA:1124,0
DA:1126,0
DA:1155,1
DA:1156,2
DA:1157,1
DA:1158,0
DA:1160,3
DA:1161,1
DA:1165,1
DA:1166,2
DA:1167,2
DA:1169,3
DA:1173,1
DA:1121,0
DA:1123,0
DA:1152,1
DA:1153,2
DA:1154,1
DA:1155,0
DA:1157,3
DA:1158,1
DA:1162,1
DA:1163,2
DA:1164,2
DA:1166,3
DA:1170,1
DA:1171,2
DA:1172,0
DA:1174,2
DA:1175,0
DA:1177,2
DA:1190,1
DA:1187,1
DA:1188,2
DA:1189,2
DA:1190,2
DA:1191,2
DA:1192,2
DA:1193,2
DA:1194,2
DA:1195,5
DA:1198,1
DA:1200,2
DA:1202,4
DA:1205,0
DA:1232,1
DA:1233,1
DA:1234,1
DA:1235,6
DA:1247,1
DA:1249,0
DA:1264,1
DA:1192,5
DA:1195,1
DA:1197,2
DA:1199,4
DA:1202,0
DA:1229,1
DA:1230,1
DA:1231,1
DA:1232,6
DA:1244,1
DA:1246,0
DA:1261,1
DA:1262,1
DA:1265,1
DA:1268,1
DA:1269,3
DA:1281,1
DA:1282,1
DA:1285,0
DA:1266,3
DA:1278,1
DA:1279,1
DA:1282,0
DA:1283,0
DA:1284,0
DA:1286,0
DA:1287,0
DA:1289,0
DA:1293,0
DA:1290,0
DA:1291,0
DA:1292,0
DA:1294,0
DA:1295,0
DA:1297,0
DA:1311,1
DA:1312,1
DA:1313,3
DA:1314,2
DA:1315,5
DA:1316,4
DA:1318,1
DA:1319,3
DA:1308,2
DA:1309,2
DA:1310,6
DA:1311,4
DA:1312,12
DA:1313,8
DA:1315,1
DA:1316,3
DA:1317,1
DA:1320,1
DA:1323,2
DA:1325,1
DA:1326,9
LF:422
LH:229
DA:1322,1
DA:1323,4
LF:420
LH:222
end_of_record
+1 −0
Original line number Diff line number Diff line
@@ -29,3 +29,4 @@ tracing-log = { workspace = true }
tracing-subscriber = { workspace = true }
uriparse = { workspace = true }
sha2 = "0.10.8"
which = "6.0.3"
+16 −19
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ use tracing::{debug, error, info, trace, warn};
use uriparse::Scheme;
use uriparse::URI;
use valuable_derive::Valuable;
use which::which;

use std::process;

@@ -385,26 +386,22 @@ impl Command {
    /// assert!(command.test());
    /// ```
    pub fn test(self) -> bool {
        let name = if cfg!(target_os = "windows") { "pwsh" } else { "which" };
        let arguments = if cfg!(target_os = "windows") {
            to_string(vec!["-NoProfile", "-Command", "command", &self.name])
        } else {
            vec![self.name]
        };
        let command = Command::init()
            .name(name.to_owned())
            .maybe_virtual_environment(self.virtual_environment)
            .build();
        let script = Script::init().command(command).arguments(arguments).build();
        let output = script.run();
        let path = output.into_iter().last().unwrap_or_default().trim().to_string();
        match PathBuf::from(path.clone()).try_exists() {
        match which(&self.name) {
            | Ok(value) => {
                let path = value.to_str().unwrap();
                match value.clone().try_exists() {
                    | Ok(true) => {
                info!(path, "=> {} Command", Label::found());
                        debug!(path, "=> {} Command test", Label::found());
                        true
                    }
                    | _ => {
                error!(path, "=> {} Command", Label::not_found());
                        debug!(path, "=> {} Command test", Label::not_found());
                        false
                    }
                }
            }
            | Err(_) => {
                error!(name = self.name, "=> {} Command test", Label::fail());
                false
            }
        }
+4 −4
Original line number Diff line number Diff line
@@ -23,16 +23,16 @@ fn test_add_forward_slash() {

#[test]
fn test_command() {
    let nonexistent: Command = Command::init().name("does-not-exist".to_string()).build();
    assert!(!nonexistent.test());
    let python = Command::init().name("python".to_string()).virtual_environment("base".to_string()).build();
    assert_eq!(python.name, "python".to_string());
    assert_eq!(python.virtual_environment, Some("base".to_string()));
    let command = Command::init().name("ls".to_string()).build();
    assert!(command.test());
    let nonexistent: Command = Command::init().name("does-not-exist".to_string()).build();
    assert!(!nonexistent.test());
    if cfg!(target_os = "windows") {
        // WIP
    } else {
        let command = Command::init().name("ls".to_string()).build();
        assert!(command.test());
        let cargo = Command::init().name("cargo".to_string()).build();
        assert!(cargo.test());
        // Using conda virtual environment
Loading