Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added test coverage for cleanup method #310

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package software.aws.toolkits.eclipse.amazonq.lsp.manager.fetcher;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.FileNotFoundException;
Expand Down Expand Up @@ -291,6 +293,65 @@ void fetchWhenMultipleVersionsChooseLatest() throws IOException, InterruptedExce
assertTrue(zipContentsMatchUnzipped(zipPath, unzippedPath));
}

@Test
void testCleanup() throws IOException {

//set up compatible versions
String sampleLspVersionV2 = String.format("%s.1.0", versionRange.getLeft().getMajor());
String sampleLspVersionV3 = String.format("%s.2.0", versionRange.getLeft().getMajor());
sampleManifest = createManifest(
List.of(
sampleLspVersion,
createLspVersion(sampleLspVersionV2),
createLspVersion(sampleLspVersionV3))
);

//create the compatible versions
Path acceptedVersion1 = Files.createDirectory(tempDir.resolve("1.7.0"));
Path acceptedVersion2 = Files.createDirectory(tempDir.resolve("1.2.0"));

//create delisted versions
Path delistedVersion1 = Files.createDirectory(tempDir.resolve("2.0.0"));
Path delistedVersion2 = Files.createDirectory(tempDir.resolve("1.0.0"));

//create extra version
Path extraVersion1 = Files.createDirectory(tempDir.resolve("1.1.0"));

//verify existence of files
assertTrue(Files.exists(acceptedVersion1));
assertTrue(Files.exists(acceptedVersion2));
assertTrue(Files.exists(extraVersion1));
assertTrue(Files.exists(delistedVersion1));
assertTrue(Files.exists(delistedVersion2));

lspFetcher = createFetcher();
lspFetcher.cleanup(tempDir);

//verify compatible versions still exist
assertTrue(Files.exists(acceptedVersion1));
assertTrue(Files.exists(acceptedVersion2));

//verify delisted versions were deleted
assertFalse(Files.exists(delistedVersion1));
assertFalse(Files.exists(delistedVersion2));
verify(mockLogger).info("Cleaning up 2 cached de-listed versions for Amazon Q Language Server");

//verify extra versions were deleted
assertFalse(Files.exists(extraVersion1));
verify(mockLogger).info("Cleaning up 1 cached extra versions for Amazon Q Language Server");
}

@Test
void testCleanupNullManifest() throws IOException {
sampleManifest = null;
Path delistedVersion1 = Files.createDirectory(tempDir.resolve("1.0.0"));
lspFetcher = createFetcher();
lspFetcher.cleanup(tempDir);

assertTrue(Files.exists(tempDir));
assertTrue(Files.exists(delistedVersion1));
}

private HttpResponse<Path> createMockHttpResponse(final Path file, final int statusCode) {
@SuppressWarnings("unchecked")
HttpResponse<Path> response = mock(HttpResponse.class);
Expand Down
Loading