Skip to content

Commit e2f5997

Browse files
authored
fix dry-run output and add tests, fixes #15730 (#15754)
1 parent 3224e86 commit e2f5997

2 files changed

Lines changed: 191 additions & 28 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/DryRunTemplateManager.java

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
import java.io.File;
77
import java.io.IOException;
8+
import java.nio.charset.StandardCharsets;
89
import java.nio.file.Path;
10+
import java.nio.file.Paths;
911
import java.util.Collections;
1012
import java.util.HashMap;
1113
import java.util.Map;
@@ -45,29 +47,35 @@ public Map<String, DryRunStatus> getDryRunStatusMap() {
4547
*/
4648
@Override
4749
public File write(Map<String, Object> data, String template, File target) throws IOException {
48-
if (this.options.isSkipOverwrite() && target.exists()) {
49-
dryRunStatusMap.put(target.toString(),
50-
new DryRunStatus(
51-
target.toPath(),
52-
DryRunStatus.State.SkippedOverwrite,
53-
"File exists and skip overwrite option is enabled."
54-
));
55-
}
56-
57-
return target;
50+
return writeToFile(target.getAbsolutePath(), "dummy".getBytes(StandardCharsets.UTF_8));
5851
}
5952

6053
@Override
6154
public File writeToFile(String filename, byte[] contents) throws IOException {
62-
Path path = java.nio.file.Paths.get(filename);
55+
final Path path = Paths.get(filename);
56+
final File outputFile = path.toFile();
6357
DryRunStatus status = new DryRunStatus(path);
64-
if (this.options.isMinimalUpdate()) {
58+
59+
if (outputFile.exists()) {
60+
if (this.options.isSkipOverwrite()) {
61+
status = new DryRunStatus(
62+
path,
63+
DryRunStatus.State.SkippedOverwrite,
64+
"File exists and skip overwrite option is enabled."
65+
);
66+
} else if (this.options.isMinimalUpdate()) {
67+
status.setState(DryRunStatus.State.WriteIfNewer);
68+
} else {
69+
status.setState(DryRunStatus.State.Write);
70+
}
71+
} else if (this.options.isMinimalUpdate()) {
6572
status.setState(DryRunStatus.State.WriteIfNewer);
6673
} else {
6774
status.setState(DryRunStatus.State.Write);
6875
}
6976
dryRunStatusMap.put(filename, status);
70-
return path.toFile();
77+
78+
return outputFile;
7179
}
7280

7381
@Override
@@ -82,26 +90,15 @@ public void ignore(Path path, String context) {
8290

8391
@Override
8492
public void skip(Path path, String context) {
93+
final DryRunStatus status = new DryRunStatus(path, DryRunStatus.State.Skipped, context);
8594
if (this.options.isSkipOverwrite() && path.toFile().exists()) {
86-
dryRunStatusMap.put(path.toString(),
87-
new DryRunStatus(
88-
path,
89-
DryRunStatus.State.SkippedOverwrite,
90-
context
91-
));
92-
return;
95+
status.setState(DryRunStatus.State.SkippedOverwrite);
9396
}
94-
95-
dryRunStatusMap.put(path.toString(),
96-
new DryRunStatus(
97-
path,
98-
DryRunStatus.State.Skipped,
99-
context
100-
));
97+
dryRunStatusMap.put(path.toString(), status);
10198
}
10299

103100
@Override
104101
public void error(Path path, String context) {
105-
dryRunStatusMap.put(path.toString(), new DryRunStatus(path, DryRunStatus.State.Error));
102+
dryRunStatusMap.put(path.toString(), new DryRunStatus(path, DryRunStatus.State.Error, context));
106103
}
107104
}
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
package org.openapitools.codegen;
2+
3+
import org.openapitools.codegen.templating.TemplateManagerOptions;
4+
import org.testng.annotations.Test;
5+
6+
import java.io.File;
7+
import java.io.IOException;
8+
import java.nio.file.Path;
9+
import java.util.HashMap;
10+
import java.util.Map;
11+
12+
import static org.testng.Assert.*;
13+
14+
public class DryRunTemplateManagerTest {
15+
@Test
16+
public void testError() throws IOException {
17+
final DryRunTemplateManager templateManager = getTemplateManager(false, false);
18+
final File tempFile = File.createTempFile("dryrun-test", ".txt");
19+
tempFile.deleteOnExit();
20+
21+
templateManager.error(tempFile.toPath(), "errored");
22+
final Map<String, DryRunStatus> result = templateManager.getDryRunStatusMap();
23+
24+
assertEquals(result.size(), 1);
25+
assertDryRunStatus(
26+
result.get(tempFile.getAbsolutePath()),
27+
tempFile.toPath(),
28+
DryRunStatus.State.Error,
29+
"errored"
30+
);
31+
}
32+
33+
@Test
34+
public void testIgnore() throws IOException {
35+
final DryRunTemplateManager templateManager = getTemplateManager(false, false);
36+
final File tempFile = File.createTempFile("dryrun-test", ".txt");
37+
tempFile.deleteOnExit();
38+
39+
templateManager.ignore(tempFile.toPath(), "ignored");
40+
final Map<String, DryRunStatus> result = templateManager.getDryRunStatusMap();
41+
42+
assertEquals(result.size(), 1);
43+
assertDryRunStatus(
44+
result.get(tempFile.getAbsolutePath()),
45+
tempFile.toPath(),
46+
DryRunStatus.State.Ignored,
47+
"ignored"
48+
);
49+
}
50+
51+
@Test
52+
public void testWrite() throws IOException {
53+
DryRunTemplateManager templateManager = getTemplateManager(false, false);
54+
final File tempFile = File.createTempFile("dryrun-test", ".txt");
55+
final Map<String, Object> dummyData = new HashMap<>();
56+
57+
templateManager.write(dummyData, "dummy", tempFile);
58+
Map<String, DryRunStatus> result = templateManager.getDryRunStatusMap();
59+
60+
assertEquals(result.size(), 1);
61+
assertDryRunStatus(
62+
result.get(tempFile.getAbsolutePath()),
63+
tempFile.toPath(),
64+
DryRunStatus.State.Write,
65+
"File will be written."
66+
);
67+
68+
templateManager = getTemplateManager(true, false);
69+
templateManager.write(dummyData, "dummy", tempFile);
70+
result = templateManager.getDryRunStatusMap();
71+
72+
assertEquals(result.size(), 1);
73+
assertDryRunStatus(
74+
result.get(tempFile.getAbsolutePath()),
75+
tempFile.toPath(),
76+
DryRunStatus.State.WriteIfNewer,
77+
"File will be written only if it is new or if contents differ from an existing file."
78+
);
79+
80+
templateManager = getTemplateManager(true, true);
81+
templateManager.write(dummyData, "dummy", tempFile);
82+
result = templateManager.getDryRunStatusMap();
83+
84+
assertEquals(result.size(), 1);
85+
assertDryRunStatus(
86+
result.get(tempFile.getAbsolutePath()),
87+
tempFile.toPath(),
88+
DryRunStatus.State.SkippedOverwrite,
89+
"File exists and skip overwrite option is enabled."
90+
);
91+
92+
tempFile.delete();
93+
94+
templateManager = getTemplateManager(true, false);
95+
templateManager.write(dummyData, "dummy", tempFile);
96+
result = templateManager.getDryRunStatusMap();
97+
98+
assertEquals(result.size(), 1);
99+
assertDryRunStatus(
100+
result.get(tempFile.getAbsolutePath()),
101+
tempFile.toPath(),
102+
DryRunStatus.State.WriteIfNewer,
103+
"File will be written only if it is new or if contents differ from an existing file."
104+
);
105+
106+
templateManager = getTemplateManager(false, false);
107+
templateManager.write(dummyData, "dummy", tempFile);
108+
result = templateManager.getDryRunStatusMap();
109+
110+
assertEquals(result.size(), 1);
111+
assertDryRunStatus(
112+
result.get(tempFile.getAbsolutePath()),
113+
tempFile.toPath(),
114+
DryRunStatus.State.Write,
115+
"File will be written."
116+
);
117+
}
118+
119+
@Test
120+
public void testSkip() throws IOException {
121+
DryRunTemplateManager templateManager = getTemplateManager(false, false);
122+
final File tempFile = File.createTempFile("dryrun-test", ".txt");
123+
tempFile.deleteOnExit();
124+
125+
templateManager.skip(tempFile.toPath(), "skipped");
126+
Map<String, DryRunStatus> result = templateManager.getDryRunStatusMap();
127+
128+
assertEquals(result.size(), 1);
129+
assertDryRunStatus(
130+
result.get(tempFile.getAbsolutePath()),
131+
tempFile.toPath(),
132+
DryRunStatus.State.Skipped,
133+
"skipped"
134+
);
135+
136+
templateManager = getTemplateManager(false, true);
137+
138+
templateManager.skip(tempFile.toPath(), "skipped");
139+
result = templateManager.getDryRunStatusMap();
140+
141+
assertEquals(result.size(), 1);
142+
assertDryRunStatus(
143+
result.get(tempFile.getAbsolutePath()),
144+
tempFile.toPath(),
145+
DryRunStatus.State.SkippedOverwrite,
146+
"File is configured not to overwrite an existing file of the same name."
147+
);
148+
}
149+
150+
private DryRunTemplateManager getTemplateManager(final boolean minimalUpdate, final boolean skipOverwrite) {
151+
return new DryRunTemplateManager(
152+
new TemplateManagerOptions(minimalUpdate, skipOverwrite)
153+
);
154+
}
155+
156+
private void assertDryRunStatus(
157+
final DryRunStatus dryRunStatus,
158+
final Path path,
159+
final DryRunStatus.State state,
160+
final String reason
161+
) {
162+
assertEquals(dryRunStatus.getPath().toString(), path.toString());
163+
assertEquals(dryRunStatus.getState().toString(), state.toString());
164+
assertEquals(dryRunStatus.getReason(), reason);
165+
}
166+
}

0 commit comments

Comments
 (0)