|
| 1 | +package org.codejive.jpm; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.*; |
| 4 | + |
| 5 | +import java.io.ByteArrayOutputStream; |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.PrintStream; |
| 8 | +import java.nio.file.Files; |
| 9 | +import java.nio.file.Path; |
| 10 | +import org.junit.jupiter.api.AfterEach; |
| 11 | +import org.junit.jupiter.api.BeforeEach; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import org.junit.jupiter.api.io.TempDir; |
| 14 | +import org.junitpioneer.jupiter.ClearEnvironmentVariable; |
| 15 | +import org.junitpioneer.jupiter.SetEnvironmentVariable; |
| 16 | +import picocli.CommandLine; |
| 17 | + |
| 18 | +/** Integration tests for the --cache option and JPM_CACHE environment variable. */ |
| 19 | +class MainCacheIntegrationTest { |
| 20 | + |
| 21 | + @TempDir Path tempDir; |
| 22 | + @TempDir Path cacheDir1; |
| 23 | + @TempDir Path cacheDir2; |
| 24 | + |
| 25 | + private String originalUserDir; |
| 26 | + private PrintStream originalOut; |
| 27 | + private PrintStream originalErr; |
| 28 | + private ByteArrayOutputStream outContent; |
| 29 | + private ByteArrayOutputStream errContent; |
| 30 | + |
| 31 | + @BeforeEach |
| 32 | + void setUp() { |
| 33 | + originalUserDir = System.getProperty("user.dir"); |
| 34 | + System.setProperty("user.dir", tempDir.toString()); |
| 35 | + System.setProperty("picocli.ansi", "false"); |
| 36 | + |
| 37 | + // Capture stdout and stderr |
| 38 | + originalOut = System.out; |
| 39 | + originalErr = System.err; |
| 40 | + outContent = new ByteArrayOutputStream(); |
| 41 | + errContent = new ByteArrayOutputStream(); |
| 42 | + System.setOut(new PrintStream(outContent)); |
| 43 | + System.setErr(new PrintStream(errContent)); |
| 44 | + } |
| 45 | + |
| 46 | + @AfterEach |
| 47 | + void tearDown() { |
| 48 | + System.setProperty("user.dir", originalUserDir); |
| 49 | + System.setOut(originalOut); |
| 50 | + System.setErr(originalErr); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + @ClearEnvironmentVariable(key = "JPM_CACHE") |
| 55 | + void testPathCommandWithCacheOption() throws IOException { |
| 56 | + // Create app.yml |
| 57 | + createSimpleAppYml(); |
| 58 | + |
| 59 | + CommandLine cmd = Main.getCommandLine(); |
| 60 | + int exitCode = cmd.execute("path", "--cache", cacheDir1.toString()); |
| 61 | + |
| 62 | + // Exit code 0 or 1 is acceptable (1 means dependency not found, which is expected) |
| 63 | + assertThat(exitCode).isIn(0, 1); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + @SetEnvironmentVariable(key = "JPM_CACHE", value = "/tmp/env-cache") |
| 68 | + void testPathCommandWithEnvironmentVariable() throws IOException { |
| 69 | + // Create app.yml |
| 70 | + createSimpleAppYml(); |
| 71 | + |
| 72 | + CommandLine cmd = Main.getCommandLine(); |
| 73 | + int exitCode = cmd.execute("path"); |
| 74 | + |
| 75 | + // The command should succeed with JPM_CACHE set |
| 76 | + assertThat(exitCode).isIn(0, 1); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + @SetEnvironmentVariable(key = "JPM_CACHE", value = "/tmp/env-cache") |
| 81 | + void testCopyCommandCacheOptionOverridesEnvironmentVariable() throws IOException { |
| 82 | + CommandLine cmd = Main.getCommandLine(); |
| 83 | + int exitCode = |
| 84 | + cmd.execute( |
| 85 | + "copy", "--cache", cacheDir1.toString(), "--quiet", "fake:artifact:1.0.0"); |
| 86 | + |
| 87 | + // The command should use cacheDir1 (from --cache) not /tmp/env-cache |
| 88 | + // Even though it will fail to resolve, it should parse correctly |
| 89 | + assertThat(exitCode).isIn(0, 1); // May fail to resolve, but shouldn't crash |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + @ClearEnvironmentVariable(key = "JPM_CACHE") |
| 94 | + void testInstallCommandWithShortCacheOption() throws IOException { |
| 95 | + createSimpleAppYml(); |
| 96 | + |
| 97 | + CommandLine cmd = Main.getCommandLine(); |
| 98 | + int exitCode = |
| 99 | + cmd.execute( |
| 100 | + "install", "-c", cacheDir1.toString(), "--quiet", "fake:artifact:1.0.0"); |
| 101 | + |
| 102 | + // The -c short form should work the same as --cache |
| 103 | + assertThat(exitCode).isIn(0, 1); // May fail to resolve, but shouldn't crash |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + void testCacheOptionInHelp() { |
| 108 | + CommandLine cmd = Main.getCommandLine(); |
| 109 | + int exitCode = cmd.execute("copy", "--help"); |
| 110 | + |
| 111 | + // PicoCLI may return 0 or 2 for help depending on configuration |
| 112 | + // What matters is that the help text is displayed |
| 113 | + String output = outContent.toString() + errContent.toString(); |
| 114 | + assertThat(output) |
| 115 | + .contains("-c, --cache") |
| 116 | + .contains("Directory where downloaded artifacts will be cached") |
| 117 | + .contains("JPM_CACHE"); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + @SetEnvironmentVariable(key = "JPM_CACHE", value = " ") |
| 122 | + void testGetCacheDirWithWhitespaceOnlyEnvironmentVariable() throws IOException { |
| 123 | + // An environment variable with only whitespace should be treated as empty |
| 124 | + createSimpleAppYml(); |
| 125 | + |
| 126 | + CommandLine cmd = Main.getCommandLine(); |
| 127 | + // This should not crash - whitespace-only JPM_CACHE should be ignored |
| 128 | + int exitCode = cmd.execute("path"); |
| 129 | + |
| 130 | + assertThat(exitCode).isIn(0, 1); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + @ClearEnvironmentVariable(key = "JPM_CACHE") |
| 135 | + void testCacheOptionWithRelativePath() throws IOException { |
| 136 | + createSimpleAppYml(); |
| 137 | + |
| 138 | + CommandLine cmd = Main.getCommandLine(); |
| 139 | + int exitCode = cmd.execute("path", "--cache", "./my-cache"); |
| 140 | + |
| 141 | + assertThat(exitCode).isIn(0, 1); |
| 142 | + // Should accept relative paths |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + @ClearEnvironmentVariable(key = "JPM_CACHE") |
| 147 | + void testCacheOptionWithAbsolutePath() throws IOException { |
| 148 | + createSimpleAppYml(); |
| 149 | + |
| 150 | + CommandLine cmd = Main.getCommandLine(); |
| 151 | + int exitCode = cmd.execute("path", "--cache", cacheDir1.toAbsolutePath().toString()); |
| 152 | + |
| 153 | + assertThat(exitCode).isIn(0, 1); |
| 154 | + // Should accept absolute paths |
| 155 | + } |
| 156 | + |
| 157 | + private void createSimpleAppYml() throws IOException { |
| 158 | + String yamlContent = |
| 159 | + "dependencies:\n" |
| 160 | + + " fake:dummy: \"1.2.3\"\n" |
| 161 | + + "\n" |
| 162 | + + "actions:\n" |
| 163 | + + " build: \"echo building\"\n"; |
| 164 | + Files.writeString(tempDir.resolve("app.yml"), yamlContent); |
| 165 | + } |
| 166 | +} |
0 commit comments