@@ -69,7 +69,10 @@ impl OutputProcessor for CliTokenResponse {
6969/// Authenticates the identity logged in to the [Azure CLI](https://learn.microsoft.com/cli/azure/what-is-azure-cli).
7070#[ derive( Debug ) ]
7171pub struct AzureCliCredential {
72- options : AzureCliCredentialOptions ,
72+ env : Env ,
73+ executor : Arc < dyn Executor > ,
74+ subscription : Option < String > ,
75+ tenant_id : Option < String > ,
7376}
7477
7578/// Options for constructing an [`AzureCliCredential`].
@@ -97,27 +100,31 @@ pub struct AzureCliCredentialOptions {
97100 /// you can supply your own implementation using a different asynchronous runtime.
98101 pub executor : Option < Arc < dyn Executor > > ,
99102
103+ #[ cfg( test) ]
100104 env : Option < Env > ,
101105}
102106
103107impl AzureCliCredential {
104108 /// Create a new `AzureCliCredential`.
105109 pub fn new ( options : Option < AzureCliCredentialOptions > ) -> azure_core:: Result < Arc < Self > > {
106- let mut options = options. unwrap_or_default ( ) ;
110+ let options = options. unwrap_or_default ( ) ;
107111 if let Some ( ref tenant_id) = options. tenant_id {
108112 validate_tenant_id ( tenant_id) ?;
109113 }
110114 if let Some ( ref subscription) = options. subscription {
111115 validate_subscription ( subscription) ?;
112116 }
113- if options. env . is_none ( ) {
114- options. env = Some ( Env :: default ( ) ) ;
115- }
116- if options. executor . is_none ( ) {
117- options. executor = Some ( new_executor ( ) ) ;
118- }
119-
120- Ok ( Arc :: new ( Self { options } ) )
117+ #[ cfg( test) ]
118+ let env = options. env . unwrap_or_default ( ) ;
119+ #[ cfg( not( test) ) ]
120+ let env = Env :: default ( ) ;
121+
122+ Ok ( Arc :: new ( Self {
123+ env,
124+ executor : options. executor . unwrap_or ( new_executor ( ) ) ,
125+ subscription : options. subscription ,
126+ tenant_id : options. tenant_id ,
127+ } ) )
121128 }
122129}
123130
@@ -140,25 +147,19 @@ impl TokenCredential for AzureCliCredential {
140147
141148 let mut command = OsString :: from ( "az account get-access-token -o json --scope " ) ;
142149 command. push ( scopes[ 0 ] ) ;
143- if let Some ( ref tenant_id) = self . options . tenant_id {
150+ if let Some ( ref tenant_id) = self . tenant_id {
144151 command. push ( " --tenant " ) ;
145152 command. push ( tenant_id) ;
146153 }
147- if let Some ( ref subscription) = self . options . subscription {
154+ if let Some ( ref subscription) = self . subscription {
148155 command. push ( r#" --subscription ""# ) ;
149156 command. push ( subscription) ;
150157 command. push ( "\" " ) ;
151158 }
152159
153160 trace ! ( "running Azure CLI command: {command:?}" ) ;
154161
155- shell_exec :: < CliTokenResponse > (
156- // unwrap() is safe because new() ensured the values are Some
157- self . options . executor . clone ( ) . unwrap ( ) ,
158- self . options . env . as_ref ( ) . unwrap ( ) ,
159- & command,
160- )
161- . await
162+ shell_exec :: < CliTokenResponse > ( self . executor . clone ( ) , & self . env , & command) . await
162163 }
163164}
164165
0 commit comments