Skip to content

Commit

Permalink
Added test coverage for cleanup method (#310)
Browse files Browse the repository at this point in the history
  • Loading branch information
nborges-aws authored Jan 27, 2025
1 parent ac37ecf commit d48e7d1
Showing 1 changed file with 63 additions and 0 deletions.
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 @@ -66,9 +68,11 @@

public final class RemoteLspFetcherTest {
private static VersionRange versionRange;
private static String majorVersion;
static {
try {
versionRange = VersionRange.createFromVersionSpec("[1.0.0, 2.0.0]");
majorVersion = "1";
} catch (InvalidVersionSpecificationException e) {
throw new AmazonQPluginException("Failed to parse LSP supported version range", e);
}
Expand Down Expand Up @@ -334,6 +338,65 @@ void fetchWhenMultipleLabelVersionsChooseLatest() throws IOException, Interrupte
assertTrue(zipContentsMatchUnzipped(zipPath, unzippedPath));
}

@Test
void testCleanup() throws IOException {

//set up compatible versions
String sampleLspVersionV2 = String.format("%s.1.0", majorVersion);
String sampleLspVersionV3 = String.format("%s.2.0", majorVersion);
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

0 comments on commit d48e7d1

Please sign in to comment.