Skip to content

Commit

Permalink
Merge pull request #485 from kiatt210/#480
Browse files Browse the repository at this point in the history
#480 Add '--dryrun' mode to the 'download sources' command
  • Loading branch information
andrii-bodnar authored Oct 4, 2022
2 parents e411e29 + 07b20d9 commit 75bb368
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class DownloadSourcesAction implements NewAction<PropertiesWithFiles, Pro
private String branchName;
private boolean debug;
private boolean reviewedOnly;
private boolean dryrun;

private Outputter out;

Expand All @@ -64,14 +65,16 @@ public DownloadSourcesAction(
boolean plainView,
String branchName,
boolean debug,
boolean reviewedOnly
boolean reviewedOnly,
boolean dryrun
) {
this.files = files;
this.noProgress = noProgress;
this.plainView = plainView;
this.branchName = branchName;
this.debug = debug;
this.reviewedOnly = reviewedOnly;
this.dryrun = dryrun;
}


Expand Down Expand Up @@ -172,14 +175,14 @@ public void act(Outputter out, PropertiesWithFiles properties, ProjectClient cli
fileDestination = filePath;
}
boolean downloaded = false;
if (!reviewedOnly) {
if (!reviewedOnly && !dryrun) {
Long fileId = filePaths.get(filePath).getId();
this.downloadFile(client, fileId, Utils.joinPaths(properties.getBasePath(), fileDestination));
isAnyFileDownloaded.set(true);
downloaded = true;
} else {
FileInfo fileInfo = filePaths.get(filePath);
if (Objects.equals(fileInfo.getBranchId(), branchId) && reviewedFiles.containsKey(fileInfo.getPath())) {
if (Objects.equals(fileInfo.getBranchId(), branchId) && reviewedFiles.containsKey(fileInfo.getPath()) && !dryrun) {
java.io.File file = reviewedFiles.get(fileInfo.getPath());
String fileTargetPath = Utils.joinPaths(properties.getBasePath(), fileDestination);
this.extractFile(file, fileTargetPath);
Expand All @@ -188,7 +191,7 @@ public void act(Outputter out, PropertiesWithFiles properties, ProjectClient cli
}
}

if (downloaded) {
if (downloaded || dryrun) {
if (!plainView) {
out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.downloaded_file"), filePath)));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ protected boolean isAnsi() {
@CommandLine.Option(names = {"--reviewed"}, descriptionKey = "params.reviewedSources")
protected boolean reviewed;

@CommandLine.Option(names = {"--dryrun"})
protected boolean dryrun;

@Override
protected NewAction<PropertiesWithFiles, ProjectClient> getAction(Actions actions) {
return new DownloadSourcesAction(new FsFiles(), noProgress, plainView, branchName, debug, reviewed);
return new DownloadSourcesAction(new FsFiles(), noProgress, plainView, branchName, debug, reviewed, dryrun);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
Expand Down Expand Up @@ -60,7 +59,7 @@ public void testDest() throws IOException {
FilesInterface files = mock(FilesInterface.class);

NewAction<PropertiesWithFiles, ProjectClient> action =
new DownloadSourcesAction(files, false, false, null, true, false);
new DownloadSourcesAction(files, false, false, null, true, false, false);
action.act(Outputter.getDefault(), pb, client);

verify(client).downloadFullProject();
Expand Down Expand Up @@ -92,7 +91,7 @@ public void testDestAndUnaryAsterisk() throws IOException {
FilesInterface files = mock(FilesInterface.class);

NewAction<PropertiesWithFiles, ProjectClient> action =
new DownloadSourcesAction(files, false, false, null, true, false);
new DownloadSourcesAction(files, false, false, null, true, false, false);
action.act(Outputter.getDefault(), pb, client);

verify(client).downloadFullProject();
Expand Down Expand Up @@ -148,7 +147,7 @@ public void testDifferentPatterns() throws IOException {
FilesInterface files = mock(FilesInterface.class);

NewAction<PropertiesWithFiles, ProjectClient> action =
new DownloadSourcesAction(files, false, false, null, true, false);
new DownloadSourcesAction(files, false, false, null, true, false, false);
action.act(Outputter.getDefault(), pb, client);

verify(client).downloadFullProject();
Expand Down Expand Up @@ -214,7 +213,7 @@ public void testWithPreserveHierarchyFalse() throws IOException {
FilesInterface files = mock(FilesInterface.class);

NewAction<PropertiesWithFiles, ProjectClient> action =
new DownloadSourcesAction(files, false, false, null, true, false);
new DownloadSourcesAction(files, false, false, null, true, false, false);
action.act(Outputter.getDefault(), pb, client);

verify(client).downloadFullProject();
Expand All @@ -236,4 +235,30 @@ public void testWithPreserveHierarchyFalse() throws IOException {
verify(files).writeToFile(eq(Utils.joinPaths(project.getBasePath(), "/android_4a.xml")), any());
verifyNoMoreInteractions(files);
}

@Test
public void testDryRun() {
PropertiesWithFiles pb = NewPropertiesWithFilesUtilBuilder
.minimalBuiltPropertiesBean(
"/values/strings.xml", "/values-%two_letters_code%/%original_file_name%",
null, "/common/%original_file_name%")
.setBasePath(project.getBasePath())
.build();

ProjectClient client = mock(ProjectClient.class);
when(client.downloadFullProject())
.thenReturn(ProjectBuilder.emptyProject(Long.parseLong(pb.getProjectId()))
.addDirectory("common", 201L, null, null)
.addFile("strings.xml", "gettext", 101L, 201L, null, "/values-%two_letters_code%/%original_file_name%").build());
URL urlMock = MockitoUtils.getMockUrl(getClass());
when(client.downloadFile(eq(101L)))
.thenReturn(urlMock);

FilesInterface files = mock(FilesInterface.class);

NewAction<PropertiesWithFiles, ProjectClient> action =
new DownloadSourcesAction(files, false, false, null, true, false,true);
action.act(Outputter.getDefault(), pb, client);

}
}

0 comments on commit 75bb368

Please sign in to comment.