Skip to content

Commit

Permalink
Fix JENKINS-27540
Browse files Browse the repository at this point in the history
Add FILE macro.
  • Loading branch information
slide committed Apr 11, 2015
1 parent d5b82a3 commit e74bf1c
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.509.3</version>
<version>1.565.3</version>
</parent>

<licenses>
Expand Down
@@ -0,0 +1,75 @@
/*
* The MIT License
*
* Copyright 2015 acearl.
*
* 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.tokenmacro.impl;

import hudson.EnvVars;
import hudson.Extension;
import hudson.model.AbstractBuild;
import hudson.model.TaskListener;
import java.io.IOException;
import org.jenkinsci.plugins.tokenmacro.DataBoundTokenMacro;
import org.jenkinsci.plugins.tokenmacro.MacroEvaluationException;

@Extension
public class WorkspaceFileMacro extends DataBoundTokenMacro {
@Parameter(required=true)
public String path = "";
@Parameter
public String fileNotFoundMessage = "ERROR: File '%s' does not exist";


public static final String MACRO_NAME = "FILE";

@Override
public boolean acceptsMacroName(String macroName) {
return macroName.equals(MACRO_NAME);
}

@Override
public String evaluate(AbstractBuild<?, ?> context, TaskListener listener, String macroName)
throws MacroEvaluationException, IOException, InterruptedException {
// do some environment variable substitution
try {
EnvVars env = context.getEnvironment(listener);
path = env.expand(path);
} catch(Exception e) {
listener.error("Error retrieving environment: %s", e.getMessage());
}

if(!context.getWorkspace().child(path).exists()) {
return String.format(fileNotFoundMessage, path);
}

try {
return context.getWorkspace().child(path).readToString();
} catch (IOException e) {
return "ERROR: File '" + path + "' could not be read";
}
}

@Override
public boolean hasNestedContent() {
return true;
}
}
Expand Up @@ -21,7 +21,6 @@
* @author acearl
*/
public class AdminEmailMacroTest {

@Rule
public final JenkinsRule j = new JenkinsRule();

Expand All @@ -34,5 +33,4 @@ public void testAdminAddressMacro() throws Exception {

assertEquals("mickey@disney.com",TokenMacro.expand(b, StreamTaskListener.fromStdout(),"${ADMIN_EMAIL}"));
}

}
Expand Up @@ -22,8 +22,6 @@ public class EnvironmentVariableMacroTest {
public void testEnvironmentVariableExpansion() throws Exception {
FreeStyleProject p = j.createFreeStyleProject("foo");
FreeStyleBuild b = p.scheduleBuild2(0).get();

listener = StreamTaskListener.fromStdout();
assertEquals("foo",TokenMacro.expand(b, listener,"${ENV,var=\"JOB_NAME\"}"));
assertEquals("foo",TokenMacro.expand(b, StreamTaskListener.fromStdout(),"${ENV,var=\"JOB_NAME\"}"));
}
}
Expand Up @@ -33,7 +33,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
});
FreeStyleBuild b = project.scheduleBuild2(0).get();

listener = new StreamTaskListener(System.out);
assertEquals("value 123",TokenMacro.expand(b, listener, "${LOG_REGEX,regex=\"Test Property (123)\",replacement=\"value \\\\1\"}"));
assertEquals("value 123",TokenMacro.expand(b, StreamTaskListener.fromStdout(),
"${LOG_REGEX,regex=\"Test Property (123)\",replacement=\"value \\\\1\"}"));
}
}
Expand Up @@ -35,8 +35,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
}
});
FreeStyleBuild b = project.scheduleBuild2(0).get();

listener = new StreamTaskListener(System.out);
assertEquals("success",TokenMacro.expand(b, listener, "${PROPFILE,file=\"test.properties\",property=\"test.property\"}"));
assertEquals("success",TokenMacro.expand(b, StreamTaskListener.fromStdout(),
"${PROPFILE,file=\"test.properties\",property=\"test.property\"}"));
}
}
@@ -0,0 +1,47 @@
package org.jenkinsci.plugins.tokenmacro.impl;

import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.TaskListener;
import hudson.util.StreamTaskListener;
import org.jvnet.hudson.test.TestBuilder;

import java.io.IOException;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import static org.junit.Assert.*;

/**
* @author Kohsuke Kawaguchi
*/
public class WorkspaceFileMacroTest {
@Rule
public JenkinsRule j = new JenkinsRule();

@Test
public void test1() throws Exception {
FreeStyleProject project = j.createFreeStyleProject();
TaskListener listener = StreamTaskListener.fromStdout();
project.getBuildersList().add(new TestBuilder() {
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
build.getWorkspace().child("foo").write("Hello, world!", "UTF-8");
return true;
}
});
FreeStyleBuild build = project.scheduleBuild2(0).get();

WorkspaceFileMacro content = new WorkspaceFileMacro();
content.path = "foo";
assertEquals("Hello, world!", content.evaluate(build, listener, WorkspaceFileMacro.MACRO_NAME));
content.path = "no-such-file";
assertEquals("ERROR: File 'no-such-file' does not exist", content.evaluate(build, listener, WorkspaceFileMacro.MACRO_NAME));
content.fileNotFoundMessage = "No '%s' for you!";
assertEquals("No 'no-such-file' for you!", content.evaluate(build, listener, WorkspaceFileMacro.MACRO_NAME));
}
}

0 comments on commit e74bf1c

Please sign in to comment.