|
1 |
| -use gix_credentials::{program::Kind, Program}; |
| 1 | +use gix_credentials::{helper, program::Kind, Program}; |
| 2 | + |
| 3 | +#[cfg(windows)] |
| 4 | +const GIT: &str = "git.exe"; |
| 5 | +#[cfg(not(windows))] |
| 6 | +const GIT: &str = "git"; |
| 7 | + |
| 8 | +#[cfg(windows)] |
| 9 | +const SH: &str = "sh"; |
| 10 | +#[cfg(not(windows))] |
| 11 | +const SH: &str = "/bin/sh"; |
2 | 12 |
|
3 | 13 | #[test]
|
4 |
| -fn script() { |
5 |
| - assert!( |
6 |
| - matches!(Program::from_custom_definition("!exe").kind, Kind::ExternalShellScript(script) if script == "exe") |
| 14 | +fn empty() { |
| 15 | + let prog = Program::from_custom_definition(""); |
| 16 | + assert!(matches!(&prog.kind, Kind::ExternalName { name_and_args } if name_and_args == "")); |
| 17 | + assert_eq!( |
| 18 | + format!("{:?}", prog.to_command(&helper::Action::Store("egal".into()))), |
| 19 | + format!(r#""{GIT}" "credential-" "store""#), |
| 20 | + "not useful, but allowed, would have to be caught elsewhere" |
| 21 | + ); |
| 22 | +} |
| 23 | + |
| 24 | +#[test] |
| 25 | +fn simple_script_in_path() { |
| 26 | + let prog = Program::from_custom_definition("!exe"); |
| 27 | + assert!(matches!(&prog.kind, Kind::ExternalShellScript(script) if script == "exe")); |
| 28 | + assert_eq!( |
| 29 | + format!("{:?}", prog.to_command(&helper::Action::Store("egal".into()))), |
| 30 | + r#""exe" "store""#, |
| 31 | + "it didn't detect anything shell-scripty, and thus doesn't use a shell" |
7 | 32 | );
|
8 | 33 | }
|
9 | 34 |
|
10 | 35 | #[test]
|
11 | 36 | fn name_with_args() {
|
12 | 37 | let input = "name --arg --bar=\"a b\"";
|
13 |
| - let expected = "git credential-name --arg --bar=\"a b\""; |
14 |
| - assert!( |
15 |
| - matches!(Program::from_custom_definition(input).kind, Kind::ExternalName{name_and_args} if name_and_args == expected) |
| 38 | + let prog = Program::from_custom_definition(input); |
| 39 | + assert!(matches!(&prog.kind, Kind::ExternalName{name_and_args} if name_and_args == input)); |
| 40 | + assert_eq!( |
| 41 | + format!("{:?}", prog.to_command(&helper::Action::Store("egal".into()))), |
| 42 | + format!(r#""{GIT}" "credential-name" "--arg" "--bar=a b" "store""#) |
| 43 | + ); |
| 44 | +} |
| 45 | + |
| 46 | +#[test] |
| 47 | +fn name_with_special_args() { |
| 48 | + let input = "name --arg --bar=~/folder/in/home"; |
| 49 | + let prog = Program::from_custom_definition(input); |
| 50 | + assert!(matches!(&prog.kind, Kind::ExternalName{name_and_args} if name_and_args == input)); |
| 51 | + assert_eq!( |
| 52 | + format!("{:?}", prog.to_command(&helper::Action::Store("egal".into()))), |
| 53 | + format!(r#""{SH}" "-c" "{GIT} credential-name --arg --bar=~/folder/in/home \"$@\"" "--" "store""#) |
16 | 54 | );
|
17 | 55 | }
|
18 | 56 |
|
19 | 57 | #[test]
|
20 | 58 | fn name() {
|
21 | 59 | let input = "name";
|
22 |
| - let expected = "git credential-name"; |
23 |
| - assert!( |
24 |
| - matches!(Program::from_custom_definition(input).kind, Kind::ExternalName{name_and_args} if name_and_args == expected) |
| 60 | + let prog = Program::from_custom_definition(input); |
| 61 | + assert!(matches!(&prog.kind, Kind::ExternalName{name_and_args} if name_and_args == input)); |
| 62 | + assert_eq!( |
| 63 | + format!("{:?}", prog.to_command(&helper::Action::Store("egal".into()))), |
| 64 | + format!(r#""{GIT}" "credential-name" "store""#), |
| 65 | + "we detect that this can run without shell, which is also more portable on windows" |
25 | 66 | );
|
26 | 67 | }
|
27 | 68 |
|
28 | 69 | #[test]
|
29 |
| -fn path_with_args() { |
| 70 | +fn path_with_args_that_definitely_need_shell() { |
30 | 71 | let input = "/abs/name --arg --bar=\"a b\"";
|
31 |
| - assert!( |
32 |
| - matches!(Program::from_custom_definition(input).kind, Kind::ExternalPath{path_and_args} if path_and_args == input) |
| 72 | + let prog = Program::from_custom_definition(input); |
| 73 | + assert!(matches!(&prog.kind, Kind::ExternalPath{path_and_args} if path_and_args == input)); |
| 74 | + assert_eq!( |
| 75 | + format!("{:?}", prog.to_command(&helper::Action::Store("egal".into()))), |
| 76 | + format!(r#""{SH}" "-c" "/abs/name --arg --bar=\"a b\" \"$@\"" "--" "store""#) |
33 | 77 | );
|
34 | 78 | }
|
35 | 79 |
|
36 | 80 | #[test]
|
37 |
| -fn path() { |
| 81 | +fn path_without_args() { |
38 | 82 | let input = "/abs/name";
|
39 |
| - assert!( |
40 |
| - matches!(Program::from_custom_definition(input).kind, Kind::ExternalPath{path_and_args} if path_and_args == input) |
| 83 | + let prog = Program::from_custom_definition(input); |
| 84 | + assert!(matches!(&prog.kind, Kind::ExternalPath{path_and_args} if path_and_args == input)); |
| 85 | + assert_eq!( |
| 86 | + format!("{:?}", prog.to_command(&helper::Action::Store("egal".into()))), |
| 87 | + r#""/abs/name" "store""#, |
| 88 | + "no shell is used" |
| 89 | + ); |
| 90 | +} |
| 91 | + |
| 92 | +#[test] |
| 93 | +fn path_with_simple_args() { |
| 94 | + let input = "/abs/name a b"; |
| 95 | + let prog = Program::from_custom_definition(input); |
| 96 | + assert!(matches!(&prog.kind, Kind::ExternalPath{path_and_args} if path_and_args == input)); |
| 97 | + assert_eq!( |
| 98 | + format!("{:?}", prog.to_command(&helper::Action::Store("egal".into()))), |
| 99 | + format!(r#""{SH}" "-c" "/abs/name a b \"$@\"" "--" "store""#), |
| 100 | + "a shell is used as well because there are arguments, and we don't do splitting ourselves. On windows, this can be a problem." |
41 | 101 | );
|
42 | 102 | }
|
0 commit comments