Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mention step name in MissingContextVariableException detail message #143

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,5 +1,6 @@
package org.jenkinsci.plugins.workflow.steps;

import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -16,9 +17,17 @@
*/
public class MissingContextVariableException extends Exception {
private final @NonNull Class<?> type;
private final @CheckForNull String functionName;

/** @deprecated use {@link #MissingContextVariableException(Class, StepDescriptor)} */
@Deprecated
public MissingContextVariableException(@NonNull Class<?> type) {
this(type, null);
}

Check warning on line 26 in src/main/java/org/jenkinsci/plugins/workflow/steps/MissingContextVariableException.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 25-26 are not covered by tests

public MissingContextVariableException(@NonNull Class<?> type, @CheckForNull StepDescriptor d) {
this.type = type;
functionName = d != null ? d.getFunctionName() : null;

Check warning on line 30 in src/main/java/org/jenkinsci/plugins/workflow/steps/MissingContextVariableException.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 30 is only partially covered, one branch is missing
}

public Class<?> getType() {
Expand All @@ -30,7 +39,11 @@
boolean first = true;
for (StepDescriptor p : getProviders()) {
if (first) {
b.append("\nPerhaps you forgot to surround the code with a step that provides this, such as: ");
b.append("\nPerhaps you forgot to surround the ");
if (functionName != null) {

Check warning on line 43 in src/main/java/org/jenkinsci/plugins/workflow/steps/MissingContextVariableException.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 43 is only partially covered, one branch is missing
b.append(functionName).append(" ");
}
b.append("step with a step that provides this, such as: ");
first = false;
} else {
b.append(", ");
Expand Down
Expand Up @@ -261,8 +261,9 @@ public final void checkContextAvailability(StepContext c) throws MissingContextV
// TODO the order here is nondeterministic; should we pick the lexicographic first? Or extend MissingContextVariableException to take a Set<Class<?>> types?
for (Class<?> type : getRequiredContext()) {
Object v = c.get(type);
if (v==null)
throw new MissingContextVariableException(type);
if (v == null) {
throw new MissingContextVariableException(type, this);
}
}
}

Expand Down
@@ -0,0 +1,44 @@
/*
* The MIT License
*
* Copyright 2023 CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package org.jenkinsci.plugins.workflow.steps;

import hudson.model.Result;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

public final class MissingContextVariableExceptionTest {

@Rule public JenkinsRule r = new JenkinsRule();

@Test public void message() throws Exception {
WorkflowJob p = r.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition("sh 'oops'", true));
r.assertLogContains("Perhaps you forgot to surround the sh step with a step that provides this, such as: node", r.buildAndAssertStatus(Result.FAILURE, p));
}

}