2727import org .jenkinsci .plugins .workflow .job .WorkflowJob ;
2828import org .jenkinsci .plugins .workflow .job .WorkflowRun ;
2929import org .junit .Before ;
30+ import org .junit .ClassRule ;
3031import org .junit .Rule ;
3132import org .junit .Test ;
3233import org .junit .rules .TemporaryFolder ;
3334import org .junit .runner .RunWith ;
3435import org .junit .runners .Parameterized ;
36+ import org .jvnet .hudson .test .BuildWatcher ;
3537import org .jvnet .hudson .test .JenkinsRule ;
3638
3739import java .io .File ;
4951
5052@ RunWith (Parameterized .class )
5153public class GitUsernamePasswordBindingTest {
54+
55+ @ ClassRule
56+ public static BuildWatcher bw = new BuildWatcher ();
57+
5258 @ Parameterized .Parameters (name = "User {0}: Password {1}: GitToolInstance {2}" )
5359 public static Collection <Object []> data () {
5460 return Arrays .asList (testData );
@@ -137,7 +143,7 @@ private String batchCheck(boolean includeCliCheck) {
137143 }
138144
139145 private String shellCheck () {
140- return "env | grep -E \" GIT_USERNAME|GIT_PASSWORD|GIT_TERMINAL_PROMPT\" > auth.txt" ;
146+ return "env | grep -E \" GIT_USERNAME|GIT_PASSWORD|GIT_TERMINAL_PROMPT\" > auth.txt; " ;
141147 }
142148
143149 @ Test
@@ -146,6 +152,7 @@ public void test_EnvironmentVariables_FreeStyleProject() throws Exception {
146152 prj .getBuildWrappersList ().add (new SecretBuildWrapper (Collections .<MultiBinding <?>>
147153 singletonList (new GitUsernamePasswordBinding (gitToolInstance .getName (), credentialID ))));
148154 prj .getBuildersList ().add (isWindows () ? new BatchFile (batchCheck (isCliGitTool ())) : new Shell (shellCheck ()));
155+ prj .getBuildersList ().add (isWindows () ? new BatchFile ("echo %GIT_USERNAME%:%GIT_PASSWORD%" ) : new Shell ("echo $GIT_USERNAME; echo $GIT_PASSWORD" ));
149156 r .configRoundtrip ((Item ) prj );
150157
151158 SecretBuildWrapper wrapper = prj .getBuildWrappersList ().get (SecretBuildWrapper .class );
@@ -164,6 +171,7 @@ public void test_EnvironmentVariables_FreeStyleProject() throws Exception {
164171 r .assertLogNotContains (this .username , b );
165172 }
166173 r .assertLogNotContains (this .password , b );
174+ r .assertLogContains ("****" , b );
167175
168176 //Assert Keys
169177 assertThat (binding .variables (b ), hasItem ("GIT_USERNAME" ));
@@ -198,8 +206,14 @@ public void test_EnvironmentVariables_PipelineJob() throws Exception {
198206 + "node {\n "
199207 + " withCredentials([" + keyword + "(credentialsId: '" + credentialID + "'" + gitToolNameArg + ")]) {\n "
200208 + " if (isUnix()) {\n "
209+ + " sh ': \" $GIT_PASSWORD\" '\n " // : will expand its parameters and do nothing with them
210+ + " sh ': \" < $GIT_PASSWORD >\" '\n "
211+ + " sh ': \" $GIT_USERNAME\" '\n "
212+ + " sh ': \" < $GIT_USERNAME >\" '\n "
201213 + " sh '" + shellCheck () + "'\n "
202214 + " } else {\n "
215+ + " bat 'echo %GIT_PASSWORD%'\n "
216+ + " bat 'echo %GIT_USERNAME%'\n "
203217 + " bat '" + batchCheck (isCliGitTool ()) + "'\n "
204218 + " }\n "
205219 + " }\n "
@@ -211,6 +225,7 @@ public void test_EnvironmentVariables_PipelineJob() throws Exception {
211225 if (credentials .isUsernameSecret ()) {
212226 r .assertLogNotContains (this .username , b );
213227 }
228+ r .assertLogContains (": ****" , b );
214229 r .assertLogNotContains (this .password , b );
215230 //Assert credential values
216231 String fileContents = r .jenkins .getWorkspaceFor (project ).child ("auth.txt" ).readToString ().trim ();
0 commit comments