-
Notifications
You must be signed in to change notification settings - Fork 2
/
RunProjectStep.groovy
131 lines (109 loc) · 4.46 KB
/
RunProjectStep.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* Copyright (c) 2021-2024 tracetronic GmbH
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package de.tracetronic.jenkins.plugins.ecutestexecution.steps
import com.google.common.collect.ImmutableSet
import de.tracetronic.jenkins.plugins.ecutestexecution.actions.RunProjectAction
import de.tracetronic.jenkins.plugins.ecutestexecution.builder.TestProjectBuilder
import de.tracetronic.jenkins.plugins.ecutestexecution.configs.TestConfig
import de.tracetronic.jenkins.plugins.ecutestexecution.model.TestResult
import de.tracetronic.jenkins.plugins.ecutestexecution.util.ValidationUtil
import hudson.AbortException
import hudson.EnvVars
import hudson.Extension
import hudson.FilePath
import hudson.Launcher
import hudson.model.Run
import hudson.model.TaskListener
import hudson.util.FormValidation
import hudson.util.IOUtils
import org.jenkinsci.plugins.workflow.steps.StepContext
import org.jenkinsci.plugins.workflow.steps.StepDescriptor
import org.jenkinsci.plugins.workflow.steps.StepExecution
import org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution
import org.kohsuke.stapler.DataBoundConstructor
import org.kohsuke.stapler.QueryParameter
/**
* Step providing the execution of ecu.test projects.
*/
class RunProjectStep extends RunTestStep {
@DataBoundConstructor
RunProjectStep(String testCasePath) {
super(testCasePath)
}
@Override
StepExecution start(StepContext context) throws Exception {
return new Execution(this, context)
}
static class Execution extends SynchronousNonBlockingStepExecution<TestResult> {
private static final long serialVersionUID = 1L
private final transient RunProjectStep step
Execution(RunProjectStep step, StepContext context) {
super(context)
this.step = step
}
@Override
protected TestResult run() throws Exception {
try {
EnvVars envVars = context.get(EnvVars.class)
String expTestCasePath = envVars.expand(step.testCasePath)
TestConfig expTestConfig = step.testConfig.expand(envVars)
checkProjectPath(expTestCasePath)
TestProjectBuilder testProject = new TestProjectBuilder(expTestCasePath, expTestConfig,
step.getExecutionConfig(), context)
TestResult result = testProject.runTest()
addBuildAction(context.get(Run.class), expTestCasePath, expTestConfig, result)
return result
} catch (Exception e) {
throw new AbortException(e.getMessage())
}
}
private void checkProjectPath(String projectFile)
throws IOException, InterruptedException, IllegalArgumentException {
if (IOUtils.isAbsolute(projectFile)) {
FilePath projectPath = new FilePath(context.get(Launcher.class).getChannel(), projectFile)
if (!projectPath.exists()) {
throw new AbortException("ecu.test project at ${projectPath.getRemote()} does not exist! " +
"Please ensure that the path is correctly set and it refers to the desired directory.")
}
}
}
private void addBuildAction(Run<?, ?> run, String testCasePath, TestConfig testConfig, TestResult testResult) {
RunProjectAction action = new RunProjectAction(testCasePath, testConfig, testResult)
run.addAction(action)
}
}
/**
* DescriptorImpl for {@link RunProjectStep}
*/
@Extension
static final class DescriptorImpl extends StepDescriptor {
@Override
String getFunctionName() {
'ttRunProject'
}
@Override
String getDisplayName() {
'[TT] Run an ecu.test project'
}
@Override
Set<? extends Class<?>> getRequiredContext() {
return ImmutableSet.of(Launcher.class, Run.class, EnvVars.class, TaskListener.class)
}
/**
* Validates the test case path.
*
* @param value the test case path
* @return the form validation
*/
FormValidation doCheckTestCasePath(@QueryParameter String value) {
FormValidation valid = ValidationUtil.validateParameterizedValue(value, true)
if (valid == FormValidation.ok()) {
return ValidationUtil.validateFileExtension(value, '.prj')
}
return valid
}
}
}