Skip to content

Commit

Permalink
Merge pull request #144 from jglick/null-hygiene
Browse files Browse the repository at this point in the history
Do not suggest `passphraseVariable: '', usernameVariable: ''` in snippet generator
  • Loading branch information
jglick authored Jul 12, 2021
2 parents 824c0c9 + 86269f1 commit 535900d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.Util;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.util.Secret;
Expand All @@ -53,8 +54,8 @@ public class SSHUserPrivateKeyBinding extends MultiBinding<SSHUserPrivateKey> {
}

@DataBoundSetter
public void setUsernameVariable(@Nonnull final String usernameVariable) {
this.usernameVariable = usernameVariable;
public void setUsernameVariable(@CheckForNull String usernameVariable) {
this.usernameVariable = Util.fixEmptyAndTrim(usernameVariable);
}

@CheckForNull
Expand All @@ -63,8 +64,8 @@ public String getUsernameVariable() {
}

@DataBoundSetter
public void setPassphraseVariable(@Nonnull final String passphraseVariable) {
this.passphraseVariable = passphraseVariable;
public void setPassphraseVariable(@CheckForNull String passphraseVariable) {
this.passphraseVariable = Util.fixEmptyAndTrim(passphraseVariable);
}

@CheckForNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ public void setUp() throws IOException {
}
}

// TODO configRoundtrip to test form, null hygiene on @DataBoundSetter

@Test
public void basics() throws Exception {
String alias = "androiddebugkey";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import hudson.util.Secret;
import org.jenkinsci.plugins.credentialsbinding.MultiBinding;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.cps.SnippetizerTester;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.jenkinsci.plugins.workflow.steps.StepConfigTester;
Expand All @@ -51,7 +52,7 @@
import org.junit.ClassRule;
import org.jvnet.hudson.test.BuildWatcher;

public class SSHUserPrivateKeyTest {
public class SSHUserPrivateKeyBindingTest {

@Rule public RestartableJenkinsRule story = new RestartableJenkinsRule();
@ClassRule public static BuildWatcher bw = new BuildWatcher();
Expand Down Expand Up @@ -119,21 +120,22 @@ public CredentialsScope getScope() {
}

@Test public void configRoundTrip() throws Exception {
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
SSHUserPrivateKey c = new DummyPrivateKey("creds", "bob", "secret", "the-key");
CredentialsProvider.lookupStores(story.j.jenkins).iterator().next().addCredentials(Domain.global(), c);
SSHUserPrivateKeyBinding binding = new SSHUserPrivateKeyBinding("keyFile", "creds");
binding.setPassphraseVariable("passphrase");
binding.setUsernameVariable("user");
BindingStep s = new StepConfigTester(story.j).configRoundTrip(new BindingStep(
Collections.<MultiBinding>singletonList(binding)));
story.j.assertEqualDataBoundBeans(s.getBindings(), Collections.singletonList(binding));
}
story.then(r -> {
SnippetizerTester st = new SnippetizerTester(r);
SSHUserPrivateKey c = new DummyPrivateKey("creds", "bob", "secret", "the-key");
CredentialsProvider.lookupStores(story.j.jenkins).iterator().next().addCredentials(Domain.global(), c);
SSHUserPrivateKeyBinding binding = new SSHUserPrivateKeyBinding("keyFile", "creds");
BindingStep s = new StepConfigTester(story.j).configRoundTrip(new BindingStep(Collections.<MultiBinding>singletonList(binding)));
st.assertRoundTrip(s, "withCredentials([sshUserPrivateKey(credentialsId: 'creds', keyFileVariable: 'keyFile')]) {\n // some block\n}");
r.assertEqualDataBoundBeans(s.getBindings(), Collections.singletonList(binding));
binding.setPassphraseVariable("passphrase");
binding.setUsernameVariable("user");
s = new StepConfigTester(story.j).configRoundTrip(new BindingStep(Collections.<MultiBinding>singletonList(binding)));
st.assertRoundTrip(s, "withCredentials([sshUserPrivateKey(credentialsId: 'creds', keyFileVariable: 'keyFile', passphraseVariable: 'passphrase', usernameVariable: 'user')]) {\n // some block\n}");
r.assertEqualDataBoundBeans(s.getBindings(), Collections.singletonList(binding));
});
}


@Test public void basics() throws Exception {
final String credentialsId = "creds";
final String username = "bob";
Expand Down

0 comments on commit 535900d

Please sign in to comment.