|
| 1 | +use gix_credentials::program::main; |
| 2 | +use std::io::Cursor; |
| 3 | + |
| 4 | +#[derive(Debug, thiserror::Error)] |
| 5 | +#[error("Test error")] |
| 6 | +struct TestError; |
| 7 | + |
| 8 | +#[test] |
| 9 | +fn protocol_and_host_without_url_is_valid() { |
| 10 | + let input = b"protocol=https\nhost=github.com\n"; |
| 11 | + let mut output = Vec::new(); |
| 12 | + |
| 13 | + let mut called = false; |
| 14 | + let result = main( |
| 15 | + ["get".into()], |
| 16 | + Cursor::new(input), |
| 17 | + &mut output, |
| 18 | + |_action, context| -> Result<Option<gix_credentials::protocol::Context>, TestError> { |
| 19 | + assert_eq!(context.protocol.as_deref(), Some("https")); |
| 20 | + assert_eq!(context.host.as_deref(), Some("github.com")); |
| 21 | + assert_eq!(context.url, None, "the URL isn't automatically populated"); |
| 22 | + called = true; |
| 23 | + |
| 24 | + Ok(None) |
| 25 | + }, |
| 26 | + ); |
| 27 | + |
| 28 | + // This should fail because our mock helper returned None (no credentials found) |
| 29 | + // but it should NOT fail because of missing URL |
| 30 | + match result { |
| 31 | + Err(gix_credentials::program::main::Error::CredentialsMissing { .. }) => { |
| 32 | + assert!( |
| 33 | + called, |
| 34 | + "The helper gets called, but as nothing is provided in the function it ulimately fails" |
| 35 | + ); |
| 36 | + } |
| 37 | + other => panic!("Expected CredentialsMissing error, got: {other:?}"), |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +#[test] |
| 42 | +fn missing_protocol_with_only_host_or_protocol_fails() { |
| 43 | + for input in ["host=github.com\n", "protocol=https\n"] { |
| 44 | + let mut output = Vec::new(); |
| 45 | + |
| 46 | + let mut called = false; |
| 47 | + let result = main( |
| 48 | + ["get".into()], |
| 49 | + Cursor::new(input), |
| 50 | + &mut output, |
| 51 | + |_action, _context| -> Result<Option<gix_credentials::protocol::Context>, TestError> { |
| 52 | + called = true; |
| 53 | + Ok(None) |
| 54 | + }, |
| 55 | + ); |
| 56 | + |
| 57 | + match result { |
| 58 | + Err(gix_credentials::program::main::Error::UrlMissing) => { |
| 59 | + assert!(!called, "the context is lacking, hence nothing gets called"); |
| 60 | + } |
| 61 | + other => panic!("Expected UrlMissing error, got: {other:?}"), |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +#[test] |
| 67 | +fn url_alone_is_valid() { |
| 68 | + let input = b"url=https://github.com\n"; |
| 69 | + let mut output = Vec::new(); |
| 70 | + |
| 71 | + let mut called = false; |
| 72 | + let result = main( |
| 73 | + ["get".into()], |
| 74 | + Cursor::new(input), |
| 75 | + &mut output, |
| 76 | + |_action, context| -> Result<Option<gix_credentials::protocol::Context>, TestError> { |
| 77 | + called = true; |
| 78 | + assert_eq!(context.url.unwrap(), "https://github.com"); |
| 79 | + assert_eq!(context.host, None, "not auto-populated"); |
| 80 | + assert_eq!(context.protocol, None, "not auto-populated"); |
| 81 | + |
| 82 | + Ok(None) |
| 83 | + }, |
| 84 | + ); |
| 85 | + |
| 86 | + // This should fail because our mock helper returned None (no credentials found) |
| 87 | + // but it should NOT fail because of missing URL |
| 88 | + match result { |
| 89 | + Err(gix_credentials::program::main::Error::CredentialsMissing { .. }) => { |
| 90 | + assert!(called); |
| 91 | + } |
| 92 | + other => panic!("Expected CredentialsMissing error, got: {other:?}"), |
| 93 | + } |
| 94 | +} |
0 commit comments