詹金斯没有扩展宏

时间:2017-03-02 13:42:21

标签: java jenkins macros

我希望这是正确的术语。我现在已经打了2天了。

TL; DR

我开发的插件不会替换像%BUILD_NUMBER%%JOB_NAME%

这样的宏

详细信息

我正在为Jenkins制作一个通知程序插件,它只是在我的specflow测试中运行pickles。 (它生成一个HTML报告)。我得到了ToolInstallation以及Notifier本身作为自由式构建步骤的所有配置内容(我确实已经有了工作流位,但我还没有测试过)这个Notifier正在取代构建器运行Windows批处理文件的步骤,基本上是这个

C:\BuildTools\Pickles-2.10.0\pickles.exe ^
--feature-directory="%WORKSPACE%\My.Program.AcceptanceTests" ^
--output-directory="\\some-server\c$\Features\my-program" ^
--documentation-format=dhtml ^
--system-under-test-name="%JOB_NAME%" ^
--system-under-test-version="Build #%BUILD_NUMBER%" ^
--link-results-file="%WORKSPACE%\nunit-test-results\NUnitTestResults.xml" ^
--test-results-format=nunit

为了复制这个,我做了一个BuildPicklesNotifierArguments课程。

package hudson.plugins.pickles.utilities;

import hudson.EnvVars;
import hudson.Util;
import hudson.plugins.pickles.PicklesNotifier;
import hudson.util.ArgumentListBuilder;

import java.util.Map;

public class BuildPicklesArguments {

    private final PicklesNotifier picklesArguments;
    private final Map<String, String> buildVariables;
    private final EnvVars env;

    public BuildPicklesArguments(PicklesNotifier picklesNotifier, Map<String, String> buildVariables, EnvVars env) {
        this.picklesArguments = picklesNotifier;
        this.buildVariables = buildVariables;
        this.env = env;
    }

    public void buildArguments(ArgumentListBuilder args){
        safeAdd(args, "feature-directory", picklesArguments.getFeatureDirectory());
        safeAdd(args, "output-directory", picklesArguments.getOutputDirectory());
        safeAdd(args, "documentation-format", picklesArguments.getDocumentationFormat());
        safeAdd(args, "system-under-test-name", picklesArguments.getSystemUnderTestName());
        safeAdd(args, "system-under-test-version", picklesArguments.getSystemUnderTestVersion());
        safeAdd(args, "link-results-file", picklesArguments.getLinkResultsFiles());
        safeAdd(args, "test-results-format", picklesArguments.getTestResultFormat());

        if(!picklesArguments.getEnableComments())
            args.add("--enableComments=false");
        if(picklesArguments.getIncludeExperimentalFeatures())
            args.add("--include-experimental-features");
    }

    private void safeAdd(ArgumentListBuilder args, String switchString, String value) {
        if (value != null && !value.isEmpty()) {
            String expanded = Util.replaceMacro(value, env);
            expanded = Util.replaceMacro(expanded, buildVariables);
            args.addKeyValuePair("--", switchString, expanded, false);
//            args.addQuoted(String.format("--%s=\"%s\"", switchString, expanded));
        }
    }
}

我写了一个显示问题的单元测试

package hudson.plugins.pickles.utilities;

import hudson.EnvVars;
import hudson.plugins.pickles.PicklesNotifier;
import hudson.slaves.EnvironmentVariablesNodeProperty;
import hudson.util.ArgumentListBuilder;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;

public class BuildPicklesArgumentsTest {
    @Test
    public void buildArguments() throws Exception {
        ArgumentListBuilder args = new ArgumentListBuilder();
        Map<String, String> map = new HashMap<>();
        map.put("${BUILD_NUMBER}", "4");
        EnvironmentVariablesNodeProperty prop = new EnvironmentVariablesNodeProperty();
        EnvVars env = prop.getEnvVars();
        env.put("%BUILD_NUMBER%", "4");
        PicklesNotifier pickles = new PicklesNotifier();
        pickles.setSystemUnderTestVersion("Build #%BUILD_NUMBER%");
        pickles.setEnableComments(true);
        pickles.setIncludeExperimentalFeatures(false);
        BuildPicklesArguments build = new BuildPicklesArguments(pickles, map, env);

        build.buildArguments(args);

        assertEquals("\"--system-under-test-version=Build #4\"", args.toString());
    }
}

失败了:

Expected :"--system-under-test-version=Build #4"
Actual   :"--system-under-test-version=Build #%BUILD_NUMBER%"

我在测试中挣扎,因为我不确切知道环境变量是什么样的,所以我尝试了很多变化,但是在Jenkins中运行测试结果的结果是一样的。最后出于好奇,我取出了配置中的所有宏并重建并且它成功了(命令看起来像这样)C:\BuildTools\Pickles-2.10.0\pickles.exe --feature-directory=My.Program.AcceptanceTests --output-directory=C:\temp\testpickles --documentation-format=dhtml --system-under-test-name=Freestylnit "--system-under-test-version=Build #5" --link-results-file=NunitTestResults.xml --test-results-format=nunit。我很高兴它有效,因为我认为论证system-under-test-version会导致问题被双引号括起来,但显然这不是问题所在。仅供参考,如果您想查看其余的源代码look at it in github

0 个答案:

没有答案