private Arguments toArgList(List<String> args) { Arguments arguments = new Arguments(); if (args != null) for (String arg : args) { arguments.add(new Argument(arg)); } return arguments; }
public String[] toStringArray() { ArrayList<String> list = new ArrayList<>(this.size()); for (Argument arg : this) { list.add(arg.getValue()); } return list.toArray(new String[list.size()]); } }
@Test public void shouldAddErrorsOfEachArgumentToTheParent() { Argument argument = new Argument("invalid-argument"); argument.addError(ExecTask.ARG_LIST_STRING, "Invalid argument"); ExecTask execTask = new ExecTask("echo", new Arguments(argument), null); execTask.validate(ConfigSaveValidationContext.forChain(new BasicCruiseConfig())); assertThat(execTask.errors().on(ExecTask.ARG_LIST_STRING), is("Invalid argument")); }
public void validateTask(ValidationContext ctx) { validateCommand(); if (!usingBothArgsAndArgList()) { errors.add(ARGS, EXEC_CONFIG_ERROR); errors.add(ARG_LIST_STRING, EXEC_CONFIG_ERROR); } if (workingDirectory != null && !FilenameUtil.isNormalizedPathOutsideWorkingDir(workingDirectory)) { if (ctx.isWithinPipelines()) { errors.add(WORKING_DIR, String.format("The path of the working directory for the custom command in job '%s' in stage '%s' of pipeline '%s' is outside the agent sandbox.", ctx.getJob().name(), ctx.getStage().name(), ctx.getPipeline().name())); } else { errors.add(WORKING_DIR, String.format("The path of the working directory for the custom command in job '%s' in stage '%s' of template '%s' is outside the agent sandbox.", ctx.getJob().name(), ctx.getStage().name(), ctx.getTemplate().name())); } } for (Argument argument : getArgList()) { if (!argument.errors().isEmpty()) { errors.add(ARG_LIST_STRING, argument.errors().asString()); } } }
public void setArgsList(String[] arguments) { clearCurrentArgsAndArgList(); for (String arg : arguments) { argList.add(new Argument(arg)); } }
private String argsAsString(final String delimiter) { StringBuilder args = new StringBuilder(); for (int i = 0; i < argList.size(); ) { args.append(argList.get(i).getValue()); if (++i < argList.size()) { args.append(delimiter); } } return args.toString(); }
protected void setTaskConfigAttributes(Map attributeMap) { if (attributeMap.containsKey(COMMAND)) { command = (String) attributeMap.get(COMMAND); } if (attributeMap.containsKey(ARG_LIST_STRING)) { clearCurrentArgsAndArgList(); String value = (String) attributeMap.get(ARG_LIST_STRING); if (!StringUtils.isBlank(value)) { String[] arguments = value.split("\n"); for (String arg : arguments) { argList.add(new Argument(arg)); } } } if (attributeMap.containsKey(ARGS)) { String val = (String) attributeMap.get(ARGS); setArgs(val); } if (attributeMap.containsKey(WORKING_DIR)) { final String newWorkingDir = (String) attributeMap.get(WORKING_DIR); setWorkingDirectory(newWorkingDir); } }
private void addDescribeOfArguments(StringBuilder builder, ConfigTag configTag, GoConfigFieldWriter field) { if (field.isSubtag() && field.getValue() instanceof Arguments) { closeConfigTag(builder, field); if (field.getValue() instanceof Arguments && ((Arguments) field.getValue()).size() != 0) { for (Argument arg : (Arguments) field.getValue()) { builder.append(String.format("<arg>%s</arg>", arg.getValue())).append("\n"); } builder.append("</" + configTag.value() + ">"); } } }
@Test public void shouldSupportMultipleArgs() throws Exception { String xml = "<exec command='ls'>\n" + " <arg>arg1</arg>\n" + " <arg>arg2</arg>\n" + "</exec>"; ExecTask execTask = new MagicalGoConfigXmlLoader(new ConfigCache(), ConfigElementImplementationRegistryMother.withNoPlugins()).fromXmlPartial(xml, ExecTask.class); assertThat(execTask.getArgList(), is(new Arguments(new Argument("arg1"), new Argument("arg2")))); }
@Test public void shouldValidateConfig() throws Exception { ExecTask execTask = new ExecTask("arg1 arg2", new Arguments(new Argument("arg1"), new Argument("arg2"))); execTask.validate(ConfigSaveValidationContext.forChain(new BasicCruiseConfig())); assertThat(execTask.errors().isEmpty(), is(false)); assertThat(execTask.errors().on(ExecTask.ARGS), is(ExecTask.EXEC_CONFIG_ERROR)); assertThat(execTask.errors().on(ExecTask.ARG_LIST_STRING), is(ExecTask.EXEC_CONFIG_ERROR)); }
@Test public void shouldBeValid() throws Exception { ExecTask execTask = new ExecTask("", new Arguments(new Argument("arg1"), new Argument("arg2"))); execTask.validate(ConfigSaveValidationContext.forChain(new BasicCruiseConfig())); assertThat(execTask.errors().isEmpty(), is(true)); execTask = new ExecTask("command", "", "blah"); execTask.validate(ConfigSaveValidationContext.forChain(new BasicCruiseConfig())); assertThat(execTask.errors().isEmpty(), is(true)); }
@Test public void shouldPopulateAllAttributesOnPropertiesForDisplay() { ExecTask execTask = new ExecTask("ls", "-la", "holy/dir"); execTask.setTimeout(10l); assertThat(execTask.getPropertiesForDisplay(), hasItems(new TaskProperty("Command", "ls", "command"), new TaskProperty("Arguments", "-la", "arguments"), new TaskProperty("Working Directory", "holy/dir", "working_directory"), new TaskProperty("Timeout", "10", "timeout"))); assertThat(execTask.getPropertiesForDisplay().size(), is(4)); execTask = new ExecTask("ls", new Arguments(new Argument("-la"), new Argument("/proc")), "holy/dir"); execTask.setTimeout(10l); assertThat(execTask.getPropertiesForDisplay(), hasItems(new TaskProperty("Command", "ls", "command"), new TaskProperty("Arguments", "-la /proc", "arguments"), new TaskProperty("Working Directory", "holy/dir", "working_directory"), new TaskProperty("Timeout", "10", "timeout"))); assertThat(execTask.getPropertiesForDisplay().size(), is(4)); execTask = new ExecTask("ls", new Arguments(new Argument()), null); assertThat(execTask.getPropertiesForDisplay(), hasItems(new TaskProperty("Command", "ls", "command"))); assertThat(execTask.getPropertiesForDisplay().size(), is(1)); execTask = new ExecTask("ls", "", (String) null); assertThat(execTask.getPropertiesForDisplay(), hasItems(new TaskProperty("Command", "ls", "command"))); assertThat(execTask.getPropertiesForDisplay().size(), is(1)); }
@Test public void shouldAllowSettingOfConfigAttributes() throws Exception { ExecTask exec = new ExecTask(); exec.setConfigAttributes(m(ExecTask.COMMAND, "ls", ExecTask.ARGS, "-la", ExecTask.WORKING_DIR, "my_dir")); assertThat(exec.command(), is("ls")); assertThat(exec.getArgs(), is("-la")); assertThat(exec.getArgListString(), is("")); assertThat(exec.workingDirectory(), is("my_dir")); exec.setConfigAttributes(m(ExecTask.COMMAND, null, ExecTask.ARGS, null, ExecTask.WORKING_DIR, null)); assertThat(exec.command(), is(nullValue())); assertThat(exec.getArgs(), is("")); assertThat(exec.workingDirectory(), is(nullValue())); exec.setConfigAttributes(m(ExecTask.COMMAND, null, ExecTask.ARG_LIST_STRING, "-l\n-a\npavan's\\n working dir?", ExecTask.WORKING_DIR, null)); assertThat(exec.command(), is(nullValue())); assertThat(exec.getArgListString(), is("-l\n-a\npavan's\\n working dir?")); assertThat(exec.getArgList().size(), is(3)); assertThat(exec.getArgList().get(0), is(new Argument("-l"))); assertThat(exec.getArgList().get(1), is(new Argument("-a"))); assertThat(exec.getArgList().get(2), is(new Argument("pavan's\\n working dir?"))); assertThat(exec.workingDirectory(), is(nullValue())); }
@Test public void shouldValidateTreeForAllTasks() { AntTask antTask = antTask("build.xml", "compile", "/abc"); ExecTask execTask = new ExecTask("foo", new Arguments(new Argument("arg"))); Tasks tasks = new Tasks(antTask, execTask); String pipelineName = "p1"; PipelineConfig pipelineConfig = GoConfigMother.configWithPipelines(pipelineName).pipelineConfigByName(new CaseInsensitiveString(pipelineName)); StageConfig stageConfig = pipelineConfig.getStages().get(0); JobConfig jobConfig = stageConfig.getJobs().get(0); jobConfig.setTasks(tasks); PipelineConfigSaveValidationContext context = PipelineConfigSaveValidationContext.forChain(true, "group", pipelineConfig, stageConfig, jobConfig); assertFalse(tasks.validateTree(context)); assertThat(tasks.errors().isEmpty(), is(true)); assertThat(antTask.errors().isEmpty(), is(false)); assertThat(antTask.errors().get(AntTask.WORKING_DIRECTORY).size(), is(1)); assertThat(antTask.errors().get(AntTask.WORKING_DIRECTORY).contains("Task of job 'job' in stage 'stage' of pipeline 'p1' has path '/abc' which is outside the working directory."), is(true)); assertThat(execTask.errors().get(ExecTask.ARG_LIST_STRING).size(), is(1)); assertThat(execTask.errors().get(ExecTask.ARG_LIST_STRING).contains("Can not use both 'args' attribute and 'arg' sub element in 'exec' element!"), is(true)); assertThat(execTask.errors().get(ExecTask.ARGS).size(), is(1)); assertThat(execTask.errors().get(ExecTask.ARGS).contains("Can not use both 'args' attribute and 'arg' sub element in 'exec' element!"), is(true)); }