Skip to content

Commit

Permalink
refactor exitCode as local variable
Browse files Browse the repository at this point in the history
  • Loading branch information
alfeilex committed Jan 14, 2025
1 parent 8b91016 commit 9df8953
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ public class ProcessContextGitMock implements ProcessContext {

private final LocalDateTime now;

private int exitCode;

private final Path directory;

private ProcessResult processResult;
Expand All @@ -37,7 +35,6 @@ public ProcessContextGitMock(Path directory) {

this.arguments = new ArrayList<>();
this.processResult = new ProcessResultImpl("git", "", 0, new ArrayList<>());
this.exitCode = ProcessResult.SUCCESS;
this.directory = directory;
this.now = LocalDateTime.now();
}
Expand All @@ -51,7 +48,7 @@ public ProcessResult getProcessResult() {
}

/**
* @param exitCode the {@link #getExitCode() exit code}.
* @param exitCode th exit code.
* @param output the list of {@link OutputMessage}}
* @return the mocked {@link ProcessResult}
*/
Expand All @@ -60,22 +57,6 @@ public void setProcessResult(int exitCode, List<OutputMessage> output) {
this.processResult = new ProcessResultImpl("git", "", exitCode, output);
}

/**
* @return the mocked {@link ProcessResult#getExitCode() exit code}.
*/
public int getExitCode() {

return this.exitCode;
}

/**
* @param exitCode the {@link #getExitCode() exit code}.
*/
public void setExitCode(int exitCode) {

this.exitCode = exitCode;
}

@Override
public ProcessContext errorHandling(ProcessErrorHandling handling) {

Expand Down Expand Up @@ -121,6 +102,7 @@ public ProcessContext withPathEntry(Path path) {
@Override
public ProcessResult run(ProcessMode processMode) {

int exitCode = ProcessResult.SUCCESS;
StringBuilder command = new StringBuilder("git");
for (String arg : this.arguments) {
command.append(' ');
Expand All @@ -131,7 +113,6 @@ public ProcessResult run(ProcessMode processMode) {
if (this.arguments.contains("clean")) {
try {
Files.deleteIfExists(this.directory.resolve("new-folder"));
this.exitCode = 0;
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand All @@ -141,7 +122,6 @@ public ProcessResult run(ProcessMode processMode) {
if (Files.exists(this.directory.resolve("new-folder"))) {
OutputMessage outputMessage = new OutputMessage(false, "new-folder");
processResult.getOutputMessages().add(outputMessage);
this.exitCode = 0;
}
}
if (this.arguments.contains("clone")) {
Expand All @@ -150,14 +130,13 @@ public ProcessResult run(ProcessMode processMode) {
Path newFile = Files.createFile(gitFolderPath.resolve("url"));
// 3rd argument = repository Url
Files.writeString(newFile, this.arguments.get(2));
this.exitCode = 0;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
// always consider that files were changed
if (this.arguments.contains("diff-index")) {
this.exitCode = 1;
exitCode = 1;
}
// changes file back to initial state (uses reference file in .git folder)
if (this.arguments.contains("reset")) {
Expand All @@ -166,7 +145,7 @@ public ProcessResult run(ProcessMode processMode) {
Files.copy(gitFolderPath.resolve("objects").resolve("referenceFile"), this.directory.resolve("trackedFile"),
StandardCopyOption.REPLACE_EXISTING);
}
this.exitCode = 0;
exitCode = ProcessResult.SUCCESS;
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand All @@ -176,13 +155,13 @@ public ProcessResult run(ProcessMode processMode) {
Files.createDirectories(gitFolderPath);
Path newFile = Files.createFile(gitFolderPath.resolve("update"));
Files.writeString(newFile, this.now.toString());
this.exitCode = 0;
exitCode = ProcessResult.SUCCESS;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
this.arguments.clear();
setProcessResult(getExitCode(), this.processResult.getOutputMessages());
setProcessResult(exitCode, this.processResult.getOutputMessages());
return this.processResult;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ private IdeTestContext newGitContext(Path dir) {
this.processContext = new ProcessContextGitMock(dir);
context.setProcessContext(processContext);
context.setGitContext(new GitContextImpl(context));
// reset ProcessResult instance
this.processContext.setProcessResult(0, new ArrayList<>());
return context;
}

Expand Down

0 comments on commit 9df8953

Please sign in to comment.