|
1 | | -package com.mathworks.ci; |
2 | | - |
3 | | -/** |
4 | | - * Copyright 2019-2020 The MathWorks, Inc. Build Tester is used for unit testing to mock the actual |
5 | | - * build. |
6 | | - * |
7 | | - */ |
8 | | - |
9 | | -import java.io.IOException; |
10 | | -import java.util.ArrayList; |
11 | | -import java.util.List; |
12 | | -import javax.annotation.Nonnull; |
13 | | -import org.kohsuke.stapler.StaplerRequest; |
14 | | -import hudson.EnvVars; |
15 | | -import hudson.Extension; |
16 | | -import hudson.FilePath; |
17 | | -import hudson.Launcher; |
18 | | -import hudson.Launcher.ProcStarter; |
19 | | -import hudson.model.AbstractProject; |
20 | | -import hudson.model.Result; |
21 | | -import hudson.model.Run; |
22 | | -import hudson.model.TaskListener; |
23 | | -import hudson.tasks.BuildStepDescriptor; |
24 | | -import hudson.tasks.Builder; |
25 | | -import net.sf.json.JSONObject; |
26 | | - |
27 | | -import com.mathworks.ci.freestyle.RunMatlabCommandBuilder; |
28 | | - |
29 | | -public class RunMatlabCommandBuilderTester extends RunMatlabCommandBuilder { |
30 | | - private int buildResult; |
31 | | - private EnvVars env; |
32 | | - private MatlabReleaseInfo matlabRel; |
33 | | - private String commandParameter; |
34 | | - private String matlabExecutorPath; |
35 | | - |
36 | | - public RunMatlabCommandBuilderTester(String matlabExecutorPath, String customTestPointArgument) { |
37 | | - super(); |
38 | | - this.commandParameter = customTestPointArgument; |
39 | | - this.matlabExecutorPath = matlabExecutorPath; |
40 | | - } |
41 | | - |
42 | | - // Getter and Setters to access local members |
43 | | - |
44 | | - private void setEnv(EnvVars env) { |
45 | | - this.env = env; |
46 | | - } |
47 | | - |
48 | | - @Extension |
49 | | - public static class Desriptor extends BuildStepDescriptor<Builder> { |
50 | | - @Override |
51 | | - public String getDisplayName() { |
52 | | - return null; |
53 | | - } |
54 | | - |
55 | | - @Override |
56 | | - public boolean configure(StaplerRequest req, JSONObject formData) throws FormException { |
57 | | - save(); |
58 | | - return super.configure(req, formData); |
59 | | - } |
60 | | - |
61 | | - @Override |
62 | | - public boolean isApplicable( |
63 | | - @SuppressWarnings("rawtypes") Class<? extends AbstractProject> jobtype) { |
64 | | - return true; |
65 | | - } |
66 | | - } |
67 | | - |
68 | | - @Override |
69 | | - public void perform(@Nonnull Run<?, ?> build, @Nonnull FilePath workspace, |
70 | | - @Nonnull Launcher launcher, @Nonnull TaskListener listener) |
71 | | - throws InterruptedException, IOException { |
72 | | - |
73 | | - // Set the environment variable specific to the this build |
74 | | - setEnv(build.getEnvironment(listener)); |
75 | | - String matlabRoot = this.env.get("matlabroot"); |
76 | | - |
77 | | - // Get node specific matlabroot to get matlab version information |
78 | | - FilePath nodeSpecificMatlabRoot = new FilePath(launcher.getChannel(), matlabRoot); |
79 | | - matlabRel = new MatlabReleaseInfo(nodeSpecificMatlabRoot); |
80 | | - |
81 | | - // Invoke MATLAB command and transfer output to standard |
82 | | - // Output Console |
83 | | - |
84 | | - buildResult = execMatlabCommand(workspace, launcher, listener); |
85 | | - |
86 | | - if (buildResult != 0) { |
87 | | - build.setResult(Result.FAILURE); |
88 | | - } |
89 | | - } |
90 | | - |
91 | | - private int execMatlabCommand(FilePath workspace, Launcher launcher, |
92 | | - TaskListener listener) throws IOException, InterruptedException { |
93 | | - ProcStarter matlabLauncher; |
94 | | - try { |
95 | | - matlabLauncher = launcher.launch().pwd(workspace).envs(this.env).cmds(testMatlabCommand()).stdout(listener); |
96 | | - |
97 | | - } catch (Exception e) { |
98 | | - listener.getLogger().println(e.getMessage()); |
99 | | - return 1; |
100 | | - } |
101 | | - return matlabLauncher.join(); |
102 | | - } |
103 | | - |
104 | | - // Private custom method to pass mock MATLAB with parameters. |
105 | | - private List<String> testMatlabCommand() { |
106 | | - List<String> matlabDefaultArgs = new ArrayList<String>(); |
107 | | - matlabDefaultArgs.add(this.matlabExecutorPath); |
108 | | - matlabDefaultArgs.add(this.commandParameter); |
109 | | - return matlabDefaultArgs; |
110 | | - } |
111 | | -} |
| 1 | +package com.mathworks.ci; |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2019-2020 The MathWorks, Inc. Build Tester is used for unit testing to mock the actual |
| 5 | + * build. |
| 6 | + * |
| 7 | + */ |
| 8 | + |
| 9 | +import java.io.IOException; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.List; |
| 12 | +import javax.annotation.Nonnull; |
| 13 | +import org.kohsuke.stapler.StaplerRequest; |
| 14 | +import hudson.EnvVars; |
| 15 | +import hudson.Extension; |
| 16 | +import hudson.FilePath; |
| 17 | +import hudson.Launcher; |
| 18 | +import hudson.Launcher.ProcStarter; |
| 19 | +import hudson.model.AbstractProject; |
| 20 | +import hudson.model.Result; |
| 21 | +import hudson.model.Run; |
| 22 | +import hudson.model.TaskListener; |
| 23 | +import hudson.tasks.BuildStepDescriptor; |
| 24 | +import hudson.tasks.Builder; |
| 25 | +import net.sf.json.JSONObject; |
| 26 | + |
| 27 | +import com.mathworks.ci.freestyle.RunMatlabCommandBuilder; |
| 28 | + |
| 29 | +public class RunMatlabCommandBuilderTester extends RunMatlabCommandBuilder { |
| 30 | + private int buildResult; |
| 31 | + private EnvVars env; |
| 32 | + private MatlabReleaseInfo matlabRel; |
| 33 | + private String commandParameter; |
| 34 | + private String matlabExecutorPath; |
| 35 | + |
| 36 | + public RunMatlabCommandBuilderTester(String matlabExecutorPath, String customTestPointArgument) { |
| 37 | + super(); |
| 38 | + this.commandParameter = customTestPointArgument; |
| 39 | + this.matlabExecutorPath = matlabExecutorPath; |
| 40 | + } |
| 41 | + |
| 42 | + // Getter and Setters to access local members |
| 43 | + |
| 44 | + private void setEnv(EnvVars env) { |
| 45 | + this.env = env; |
| 46 | + } |
| 47 | + |
| 48 | + @Extension |
| 49 | + public static class Desriptor extends BuildStepDescriptor<Builder> { |
| 50 | + @Override |
| 51 | + public String getDisplayName() { |
| 52 | + return null; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public boolean configure(StaplerRequest req, JSONObject formData) throws FormException { |
| 57 | + save(); |
| 58 | + return super.configure(req, formData); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public boolean isApplicable( |
| 63 | + @SuppressWarnings("rawtypes") Class<? extends AbstractProject> jobtype) { |
| 64 | + return true; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void perform(@Nonnull Run<?, ?> build, @Nonnull FilePath workspace, |
| 70 | + @Nonnull Launcher launcher, @Nonnull TaskListener listener) |
| 71 | + throws InterruptedException, IOException { |
| 72 | + |
| 73 | + // Set the environment variable specific to the this build |
| 74 | + setEnv(build.getEnvironment(listener)); |
| 75 | + String matlabRoot = this.env.get("matlabroot"); |
| 76 | + |
| 77 | + // Get node specific matlabroot to get matlab version information |
| 78 | + FilePath nodeSpecificMatlabRoot = new FilePath(launcher.getChannel(), matlabRoot); |
| 79 | + matlabRel = new MatlabReleaseInfo(nodeSpecificMatlabRoot); |
| 80 | + |
| 81 | + // Invoke MATLAB command and transfer output to standard |
| 82 | + // Output Console |
| 83 | + |
| 84 | + buildResult = execMatlabCommand(workspace, launcher, listener); |
| 85 | + |
| 86 | + if (buildResult != 0) { |
| 87 | + build.setResult(Result.FAILURE); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private int execMatlabCommand(FilePath workspace, Launcher launcher, |
| 92 | + TaskListener listener) throws IOException, InterruptedException { |
| 93 | + ProcStarter matlabLauncher; |
| 94 | + try { |
| 95 | + matlabLauncher = launcher.launch().pwd(workspace).envs(this.env).cmds(testMatlabCommand()).stdout(listener); |
| 96 | + |
| 97 | + } catch (Exception e) { |
| 98 | + listener.getLogger().println(e.getMessage()); |
| 99 | + return 1; |
| 100 | + } |
| 101 | + return matlabLauncher.join(); |
| 102 | + } |
| 103 | + |
| 104 | + // Private custom method to pass mock MATLAB with parameters. |
| 105 | + private List<String> testMatlabCommand() { |
| 106 | + List<String> matlabDefaultArgs = new ArrayList<String>(); |
| 107 | + matlabDefaultArgs.add(this.matlabExecutorPath); |
| 108 | + matlabDefaultArgs.add(this.commandParameter); |
| 109 | + return matlabDefaultArgs; |
| 110 | + } |
| 111 | +} |
0 commit comments