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\n host=github.com\n " ;
11+ let mut output = Vec :: new ( ) ;
12+
13+ let result = main (
14+ [ "get" . into ( ) ] ,
15+ Cursor :: new ( input) ,
16+ & mut output,
17+ |_action, context| -> Result < Option < gix_credentials:: protocol:: Context > , TestError > {
18+ // Verify the context has the expected fields
19+ assert_eq ! ( context. protocol. as_deref( ) , Some ( "https" ) ) ;
20+ assert_eq ! ( context. host. as_deref( ) , Some ( "github.com" ) ) ;
21+ // Should return None to simulate no credentials found (which is expected in test)
22+ Ok ( None )
23+ } ,
24+ ) ;
25+
26+ // This should fail because our mock helper returned None (no credentials found)
27+ // but it should NOT fail because of missing URL
28+ match result {
29+ Err ( gix_credentials:: program:: main:: Error :: CredentialsMissing { .. } ) => {
30+ // This is the expected error - credentials missing, not URL missing
31+ }
32+ other => panic ! ( "Expected CredentialsMissing error, got: {:?}" , other) ,
33+ }
34+ }
35+
36+ #[ test]
37+ fn missing_protocol_with_host_fails ( ) {
38+ let input = b"host=github.com\n " ;
39+ let mut output = Vec :: new ( ) ;
40+
41+ let result = main (
42+ [ "get" . into ( ) ] ,
43+ Cursor :: new ( input) ,
44+ & mut output,
45+ |_action, _context| -> Result < Option < gix_credentials:: protocol:: Context > , TestError > { Ok ( None ) } ,
46+ ) ;
47+
48+ match result {
49+ Err ( gix_credentials:: program:: main:: Error :: UrlMissing ) => {
50+ // This is expected
51+ }
52+ other => panic ! ( "Expected UrlMissing error, got: {:?}" , other) ,
53+ }
54+ }
55+
56+ #[ test]
57+ fn missing_host_with_protocol_fails ( ) {
58+ let input = b"protocol=https\n " ;
59+ let mut output = Vec :: new ( ) ;
60+
61+ let result = main (
62+ [ "get" . into ( ) ] ,
63+ Cursor :: new ( input) ,
64+ & mut output,
65+ |_action, _context| -> Result < Option < gix_credentials:: protocol:: Context > , TestError > { Ok ( None ) } ,
66+ ) ;
67+
68+ match result {
69+ Err ( gix_credentials:: program:: main:: Error :: UrlMissing ) => {
70+ // This is expected
71+ }
72+ other => panic ! ( "Expected UrlMissing error, got: {:?}" , other) ,
73+ }
74+ }
75+
76+ #[ test]
77+ fn url_alone_is_still_valid ( ) {
78+ let input = b"url=https://github.com\n " ;
79+ let mut output = Vec :: new ( ) ;
80+
81+ let result = main (
82+ [ "get" . into ( ) ] ,
83+ Cursor :: new ( input) ,
84+ & mut output,
85+ |_action, context| -> Result < Option < gix_credentials:: protocol:: Context > , TestError > {
86+ // Verify the context has the expected fields
87+ assert_eq ! ( context. url. as_deref( ) . map( |b| & * * b) , Some ( "https://github.com" . as_bytes( ) ) ) ;
88+ // Should return None to simulate no credentials found (which is expected in test)
89+ Ok ( None )
90+ } ,
91+ ) ;
92+
93+ // This should fail because our mock helper returned None (no credentials found)
94+ // but it should NOT fail because of missing URL
95+ match result {
96+ Err ( gix_credentials:: program:: main:: Error :: CredentialsMissing { .. } ) => {
97+ // This is the expected error - credentials missing, not URL missing
98+ }
99+ other => panic ! ( "Expected CredentialsMissing error, got: {:?}" , other) ,
100+ }
101+ }
0 commit comments