Skip to content

Commit 1db6d77

Browse files
authored
Merge pull request #23 from testomatio/Issue-22_Improvement_sync
ISSUE-22 Improvement tests sync
2 parents ccf3e11 + 95d337a commit 1db6d77

7 files changed

Lines changed: 42 additions & 12 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Will dry run if apikey is not provided.
3737
>- `--directory` / `-d` - Directory to scan (optional, defaults to current directory)
3838
>- `--verbose` / `-v` - Enable verbose output (optional)
3939
>- `--dry-run` - Show what would be exported without sending (optional)
40+
>- `--keep-structure` - Prefer structure of source code over structure in Testomat.io (optional). Default: `false`
4041
4142

4243
### `sync`
@@ -51,6 +52,7 @@ Convenience command for typical workflow.
5152
>- `--apikey` / `-key` - Your Testomat.io project API key (required if you haven't provided it during this terminal session as TESTOMATIO)
5253
>- `--url` - Server URL (required if you haven't provided it during this terminal session as TESTOMATIO_URL or want to use default https://app.testomat.io)
5354
>- `--directory` / `-d` - Directory to scan (optional, defaults to current directory)
55+
>- `--keep-structure` - Prefer structure of source code over structure in Testomat.io (optional). Default: `false`
5456
5557
**Please note:** if not all the tests have been annotated with @TestId after the sync command -
5658
simply rerun the command.

src/main/java/io/testomat/commands/ImportCommand.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ public class ImportCommand implements Callable<Integer> {
5252
description = "Enable verbose output")
5353
private boolean verbose = false;
5454

55+
@Option(
56+
names = {"-s", "--keep-structure"},
57+
description = "Prefer structure of source code over structure in Testomat.io")
58+
private boolean structure = false;
59+
5560
@Option(
5661
names = {"--dry-run"},
5762
description = "Show what would be exported without sending")
@@ -106,7 +111,7 @@ public Integer call() throws Exception {
106111
ProgressBar progressBar = new ProgressBar(testFiles.size(),
107112
"Parsing " + testFiles.size() + " files");
108113
int totalExported = exportService.processTestFilesWithProgress(
109-
testFiles, apiKey, serverUrl, dryRun, verbose, progressBar);
114+
testFiles, apiKey, serverUrl, dryRun, verbose, progressBar, structure);
110115

111116
printCompletionMessage(totalExported);
112117

src/main/java/io/testomat/commands/PullIdsCommand.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public class PullIdsCommand implements Runnable {
4242
description = "Enable verbose output")
4343
private boolean verbose = false;
4444

45+
@CommandLine.Option(
46+
names = {"-s", "--keep-structure"},
47+
description = "Prefer structure of source code over structure in Testomat.io")
48+
private boolean structure = false;
49+
4550
public PullIdsCommand() {
4651
this.javaParser = new JavaParser();
4752
}

src/main/java/io/testomat/commands/SyncCommand.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
mixinStandardHelpOptions = true)
1010
public class SyncCommand implements Runnable {
1111
private static final String DEFAULT_URL = "https://app.testomat.io";
12-
private static final String VERSION = "v.0.1.8";
12+
private static final String VERSION = "v.0.1.9";
1313

1414
@CommandLine.Option(
1515
names = {"-key", "--apikey"},
@@ -36,6 +36,11 @@ public class SyncCommand implements Runnable {
3636
description = "Enable verbose output")
3737
private boolean verbose = false;
3838

39+
@CommandLine.Option(
40+
names = {"-s", "--keep-structure"},
41+
description = "Prefer structure of source code over structure in Testomat.io")
42+
private boolean structure = false;
43+
3944
@CommandLine.Spec
4045
private CommandLine.Model.CommandSpec spec;
4146

@@ -65,11 +70,13 @@ private String[] getImportArgsForCommand(String command) {
6570
"--apikey=" + apiKey,
6671
"--url=" + url,
6772
"--directory=" + directory,
73+
"--keep-structure=" + structure,
6874
"-v"}
6975
: new String[]{command,
7076
"--apikey=" + apiKey,
7177
"--url=" + url,
72-
"--directory=" + directory};
78+
"--directory=" + directory,
79+
"--keep-structure=" + structure};
7380
}
7481

7582
private void handeCommandExecution(CommandLine parent, String[] args) {

src/main/java/io/testomat/service/JsonBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ public JsonBuilder() {
1616
this.objectMapper = new ObjectMapper();
1717
}
1818

19-
public String buildRequestBody(List<TestCase> testCases, String framework) {
19+
public String buildRequestBody(List<TestCase> testCases, String framework, boolean structure) {
2020
try {
2121
ObjectNode rootNode = objectMapper.createObjectNode();
2222

2323
rootNode.put("framework", framework != null ? framework : DEFAULT_FRAMEWORK);
2424
rootNode.put("language", LANGUAGE);
2525
rootNode.put("noempty", true);
2626
rootNode.put("no-detach", true);
27-
rootNode.put("structure", true);
27+
rootNode.put("structure", structure);
2828
rootNode.put("sync", true);
2929

3030
ArrayNode testsArray = objectMapper.createArrayNode();

src/main/java/io/testomat/service/TestExportService.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,12 @@ public TestExportService(JavaFileParser fileParser, TestMethodExtractor extracto
4848

4949
public int processTestFilesWithProgress(List<File> testFiles, String apiKey,
5050
String serverUrl, boolean dryRun,
51-
boolean verbose, ProgressBar progressBar) {
51+
boolean verbose, ProgressBar progressBar,
52+
boolean structure) {
5253
ProcessingResult result = processAllFiles(testFiles, verbose, progressBar);
5354

5455
return handleProcessingResult(result.allTestCases, result.primaryFramework,
55-
apiKey, serverUrl, dryRun);
56+
apiKey, serverUrl, dryRun, structure);
5657
}
5758

5859
private List<TestCase> collectTestCasesFromFile(File file) {
@@ -73,7 +74,7 @@ private List<TestCase> collectTestCasesFromFile(File file) {
7374
}
7475

7576
private int exportAllTestCases(List<TestCase> allTestCases, String framework,
76-
String apiKey, String serverUrl) {
77+
String apiKey, String serverUrl, boolean structure) {
7778
validateExportConfig(serverUrl);
7879

7980
Stream<String> batchJsonBodies =
@@ -82,7 +83,7 @@ private int exportAllTestCases(List<TestCase> allTestCases, String framework,
8283
jsonBuilder.buildRequestBody(
8384
allTestCases.subList(i,
8485
Math.min(i + batchSize, allTestCases.size())),
85-
framework)
86+
framework, structure)
8687
);
8788

8889
String requestUrl = serverUrl + "/api/load?api_key=" + apiKey;
@@ -150,7 +151,8 @@ private ProcessingResult processAllFiles(List<File> testFiles, boolean verbose,
150151
}
151152

152153
private int handleProcessingResult(List<TestCase> allTestCases, String primaryFramework,
153-
String apiKey, String serverUrl, boolean dryRun) {
154+
String apiKey, String serverUrl, boolean dryRun,
155+
boolean structure) {
154156
if (allTestCases.isEmpty()) {
155157
log.info("No test methods found across all files");
156158
return 0;
@@ -162,7 +164,7 @@ private int handleProcessingResult(List<TestCase> allTestCases, String primaryFr
162164
printAllTestCases(allTestCases);
163165
return allTestCases.size();
164166
} else {
165-
return exportAllTestCases(allTestCases, primaryFramework, apiKey, serverUrl);
167+
return exportAllTestCases(allTestCases, primaryFramework, apiKey, serverUrl, structure);
166168
}
167169
}
168170

src/main/java/io/testomat/service/TestMethodExtractor.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ private boolean isTestMethod(MethodDeclaration method, String framework) {
4949
}
5050

5151
private TestCase createTestCase(MethodDeclaration method, String filepath, String framework) {
52+
String testId = getTestId(method).orElse("");
5253
TestCase testCase = new TestCase();
5354

54-
testCase.setName(getTestName(method));
55+
testCase.setName(getTestName(method) + testId);
5556
testCase.setCode(getMethodCode(method));
5657
testCase.setSkipped(isTestSkipped(method));
5758
testCase.setSuites(getSuites(method));
@@ -115,6 +116,14 @@ private boolean isTestSkipped(MethodDeclaration method) {
115116
return hasSkipAnnotation || hasSkipMethodName;
116117
}
117118

119+
private Optional<String> getTestId(MethodDeclaration method) {
120+
return method.getAnnotationByName("TestId")
121+
.map(ann -> " @T" + ann.asSingleMemberAnnotationExpr()
122+
.getMemberValue()
123+
.asStringLiteralExpr()
124+
.getValue());
125+
}
126+
118127
private List<String> getSuites(MethodDeclaration method) {
119128
List<String> suites = new ArrayList<>();
120129

0 commit comments

Comments
 (0)