Skip to content
This repository was archived by the owner on Mar 27, 2025. It is now read-only.

Commit 7b77260

Browse files
authored
Merge pull request #334 from mathworks/improve_teardown
Improve teardown
2 parents 6680d4e + ed3d87f commit 7b77260

File tree

8 files changed

+69
-16
lines changed

8 files changed

+69
-16
lines changed

src/main/java/com/mathworks/ci/actions/RunMatlabBuildAction.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ public void run() throws IOException, InterruptedException, MatlabExecutionExcep
8181
throw(e);
8282
} finally {
8383
annotator.forceEol();
84+
85+
try {
86+
this.runner.removeTempFolder();
87+
} catch (Exception e) {
88+
// Don't want to override more important error
89+
// thrown in catch block
90+
System.err.println(e.toString());
91+
}
8492
}
8593

8694
// Handle build result

src/main/java/com/mathworks/ci/actions/RunMatlabCommandAction.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ public void run() throws IOException, InterruptedException, MatlabExecutionExcep
3131
this.params.getTaskListener().getLogger()
3232
.println(e.getMessage());
3333
throw(e);
34-
}
34+
} finally {
35+
try {
36+
this.runner.removeTempFolder();
37+
} catch (Exception e) {
38+
// Don't want to override more important error
39+
// thrown in catch block
40+
System.err.println(e.toString());
41+
}
42+
}
3543
}
3644
}

src/main/java/com/mathworks/ci/actions/RunMatlabTestsAction.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,15 @@ public void run() throws IOException, InterruptedException, MatlabExecutionExcep
5050
.getLogger()
5151
.println(e.getMessage());
5252
throw(e);
53-
}
53+
} finally {
54+
try {
55+
this.runner.removeTempFolder();
56+
} catch (Exception e) {
57+
// Don't want to override more important error
58+
// thrown in catch block
59+
System.err.println(e.toString());
60+
}
61+
}
5462
}
5563

5664
private String singleQuotify(String in) {

src/main/java/com/mathworks/ci/utilities/MatlabCommandRunner.java

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,6 @@ public MatlabCommandRunner(MatlabActionParameters params) throws IOException, In
4848

4949
// Create temp folder
5050
this.tempFolder = matlabFolder.createTempDir("tempDir", null);
51-
52-
// If we hit an error during shutdown while cleaning up
53-
// there's not too much that we can do.
54-
Runtime.getRuntime().addShutdownHook(
55-
new Thread(() -> {
56-
try {
57-
tempFolder.deleteRecursive();
58-
} catch(Exception e) {
59-
System.err.println(e.toString());
60-
}
61-
}));
6251
}
6352

6453
/**
@@ -67,9 +56,6 @@ public MatlabCommandRunner(MatlabActionParameters params) throws IOException, In
6756
* @param command The command to run
6857
*/
6958
public void runMatlabCommand(String command) throws IOException, InterruptedException, MatlabExecutionException {
70-
71-
System.err.println("START");
72-
7359
this.params.getTaskListener().getLogger()
7460
.println("\n#################### Starting command output ####################");
7561

@@ -159,6 +145,12 @@ public FilePath getTempFolder() {
159145
return tempFolder;
160146
}
161147

148+
public void removeTempFolder() throws IOException, InterruptedException {
149+
if (tempFolder.exists()) {
150+
tempFolder.deleteRecursive();
151+
}
152+
}
153+
162154
/**
163155
* Creates a file with the specified content in the temporary folder.
164156
*

src/test/java/unit/com/mathworks/ci/actions/RunMatlabBuildActionTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,11 @@ public void shouldCopyBuildResultsToRootAndAddsAction() throws IOException, Inte
168168
// Should have copied file to root dir
169169
assertTrue(new File(tmp, "buildArtifact.json").exists());
170170
}
171+
172+
@Test
173+
public void shouldRemoveTempFolder() throws IOException, InterruptedException, MatlabExecutionException {
174+
action.run();
175+
176+
verify(runner).removeTempFolder();
177+
}
171178
}

src/test/java/unit/com/mathworks/ci/actions/RunMatlabCommandActionTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,11 @@ public void printsAndRethrowsMessage() throws IOException, InterruptedException,
6666
assertEquals(12, e.getExitCode());
6767
};
6868
}
69+
70+
@Test
71+
public void shouldRemoveTempFolder() throws IOException, InterruptedException, MatlabExecutionException {
72+
action.run();
73+
74+
verify(runner).removeTempFolder();
75+
}
6976
}

src/test/java/unit/com/mathworks/ci/actions/RunMatlabTestsActionTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,11 @@ public void printsAndRethrowsMessage() throws IOException, InterruptedException,
170170
assertEquals(12, e.getExitCode());
171171
};
172172
}
173+
174+
@Test
175+
public void shouldRemoveTempFolder() throws IOException, InterruptedException, MatlabExecutionException {
176+
action.run();
177+
178+
verify(runner).removeTempFolder();
179+
}
173180
}

src/test/java/unit/com/mathworks/ci/utilities/MatlabCommandRunnerTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,22 @@ public void correctTempFolderLocation() throws IOException, InterruptedException
9898
startsWith(expected.getRemote()));
9999
}
100100

101+
@Test
102+
public void removeTempFolderDeletesContents() throws IOException, InterruptedException {
103+
runner = new MatlabCommandRunner(params);
104+
105+
FilePath t = runner.getTempFolder();
106+
FilePath f = runner.copyFileToTempFolder("testcontent.txt", "target.txt");
107+
108+
Assert.assertTrue(t.exists());
109+
Assert.assertTrue(f.exists());
110+
111+
runner.removeTempFolder();
112+
113+
Assert.assertFalse(t.exists());
114+
Assert.assertFalse(f.exists());
115+
}
116+
101117
@Test
102118
public void prepareRunnerExecutableMaci() throws IOException, InterruptedException {
103119
runner = new MatlabCommandRunner(params);

0 commit comments

Comments
 (0)