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 (new Object [][]{
@@ -116,7 +122,7 @@ private String batchCheck(boolean includeCliCheck) {
116122 }
117123
118124 private String shellCheck () {
119- return "env | grep -E \" GIT_USERNAME|GIT_PASSWORD|GIT_TERMINAL_PROMPT\" > auth.txt" ;
125+ return "env | grep -E \" GIT_USERNAME|GIT_PASSWORD|GIT_TERMINAL_PROMPT\" > auth.txt; " ;
120126 }
121127
122128 @ Test
@@ -125,6 +131,7 @@ public void test_EnvironmentVariables_FreeStyleProject() throws Exception {
125131 prj .getBuildWrappersList ().add (new SecretBuildWrapper (Collections .<MultiBinding <?>>
126132 singletonList (new GitUsernamePasswordBinding (gitToolInstance .getName (), credentialID ))));
127133 prj .getBuildersList ().add (isWindows () ? new BatchFile (batchCheck (isCliGitTool ())) : new Shell (shellCheck ()));
134+ prj .getBuildersList ().add (isWindows () ? new BatchFile ("echo %GIT_USERNAME%:%GIT_PASSWORD%" ) : new Shell ("echo $GIT_USERNAME; echo $GIT_PASSWORD" ));
128135 r .configRoundtrip ((Item ) prj );
129136
130137 SecretBuildWrapper wrapper = prj .getBuildWrappersList ().get (SecretBuildWrapper .class );
@@ -143,6 +150,7 @@ public void test_EnvironmentVariables_FreeStyleProject() throws Exception {
143150 r .assertLogNotContains (this .username , b );
144151 }
145152 r .assertLogNotContains (this .password , b );
153+ r .assertLogContains ("****" , b );
146154
147155 //Assert Keys
148156 assertThat (binding .variables (b ), hasItem ("GIT_USERNAME" ));
@@ -177,8 +185,14 @@ public void test_EnvironmentVariables_PipelineJob() throws Exception {
177185 + "node {\n "
178186 + " withCredentials([" + keyword + "(credentialsId: '" + credentialID + "'" + gitToolNameArg + ")]) {\n "
179187 + " if (isUnix()) {\n "
188+ + " sh ': \" $GIT_PASSWORD\" '\n " // : will expand its parameters and do nothing with them
189+ + " sh ': \" < $GIT_PASSWORD >\" '\n "
190+ + " sh ': \" $GIT_USERNAME\" '\n "
191+ + " sh ': \" < $GIT_USERNAME >\" '\n "
180192 + " sh '" + shellCheck () + "'\n "
181193 + " } else {\n "
194+ + " bat 'echo %GIT_PASSWORD%'\n "
195+ + " bat 'echo %GIT_USERNAME%'\n "
182196 + " bat '" + batchCheck (isCliGitTool ()) + "'\n "
183197 + " }\n "
184198 + " }\n "
@@ -190,6 +204,7 @@ public void test_EnvironmentVariables_PipelineJob() throws Exception {
190204 if (credentials .isUsernameSecret ()) {
191205 r .assertLogNotContains (this .username , b );
192206 }
207+ r .assertLogContains (": ****" , b );
193208 r .assertLogNotContains (this .password , b );
194209 //Assert credential values
195210 String fileContents = r .jenkins .getWorkspaceFor (project ).child ("auth.txt" ).readToString ().trim ();
0 commit comments