|
| 1 | +package io.jenkins.plugins.conventionalcommits.utils; |
| 2 | + |
| 3 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 4 | +import static org.hamcrest.core.StringContains.containsString; |
| 5 | +import static org.mockito.ArgumentMatchers.any; |
| 6 | +import static org.mockito.ArgumentMatchers.eq; |
| 7 | +import static org.mockito.Mockito.verify; |
| 8 | +import static org.mockito.Mockito.when; |
| 9 | + |
| 10 | +import com.github.zafarkhaja.semver.Version; |
| 11 | +import io.jenkins.plugins.conventionalcommits.process.ProcessHelper; |
| 12 | +import java.io.File; |
| 13 | +import java.io.FileWriter; |
| 14 | +import java.io.IOException; |
| 15 | +import java.nio.file.Files; |
| 16 | +import java.nio.file.Paths; |
| 17 | +import java.util.Arrays; |
| 18 | +import org.junit.Rule; |
| 19 | +import org.junit.Test; |
| 20 | +import org.junit.rules.TemporaryFolder; |
| 21 | +import org.junit.runner.RunWith; |
| 22 | +import org.mockito.Mock; |
| 23 | +import org.mockito.junit.MockitoJUnitRunner; |
| 24 | + |
| 25 | +@RunWith(MockitoJUnitRunner.class) |
| 26 | +public class NpmProjectTypeTest { |
| 27 | + @Rule |
| 28 | + public TemporaryFolder rootFolder = new TemporaryFolder(); |
| 29 | + |
| 30 | + @Mock |
| 31 | + private ProcessHelper mockProcessHelper; |
| 32 | + |
| 33 | + /** |
| 34 | + * Helper to create a package.json file with a version. |
| 35 | + * |
| 36 | + * @param npmDir The directory where create the file |
| 37 | + * @param version The version to set |
| 38 | + * @throws Exception If errors occurs when creation the file |
| 39 | + */ |
| 40 | + private void createPackageJson(File npmDir, String version) throws Exception { |
| 41 | + Files.deleteIfExists(Paths.get(npmDir.getPath() + File.separator + "package.json")); |
| 42 | + File packageJson = rootFolder.newFile(npmDir.getName() + File.separator + "package.json"); |
| 43 | + String packageJsonContent = |
| 44 | + "{\n" + |
| 45 | + "\"name\": \"conventional-commits-plugin-example-npm\",\n" + |
| 46 | + "\"version\": " + version + ",\n" + |
| 47 | + "\"description\": \"Npm example project\"\n" + |
| 48 | + "}"; |
| 49 | + FileWriter packageWriter = new FileWriter(packageJson); |
| 50 | + packageWriter.write(packageJsonContent); |
| 51 | + packageWriter.close(); |
| 52 | + |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void shouldWriteVersionBack() throws Exception { |
| 57 | + // Set npm project |
| 58 | + File npmDir = rootFolder.newFolder("SampleNpmProject"); |
| 59 | + createPackageJson(npmDir, "1.0.0"); |
| 60 | + |
| 61 | + // Set mock for npm version command |
| 62 | + when(mockProcessHelper.runProcessBuilder(any(), any())) |
| 63 | + .then(invocationOnMock -> { |
| 64 | + createPackageJson(npmDir, "1.1.0"); |
| 65 | + return "1.1.0"; |
| 66 | + } |
| 67 | + ); |
| 68 | + NpmProjectType npmProjectType = new NpmProjectType(); |
| 69 | + |
| 70 | + npmProjectType.writeVersion(npmDir, Version.valueOf("1.1.0"), mockProcessHelper); |
| 71 | + |
| 72 | + verify(mockProcessHelper).runProcessBuilder(any(File.class), |
| 73 | + eq(Arrays.asList("npm", "version", "1.1.0"))); |
| 74 | + |
| 75 | + assertThat(new String( |
| 76 | + Files.readAllBytes(Paths.get(npmDir.getPath() + File.separator + "package.json"))), |
| 77 | + containsString("1.1.0")); |
| 78 | + } |
| 79 | + |
| 80 | + @Test(expected = IOException.class) |
| 81 | + public void shouldThrowIOException() throws Exception { |
| 82 | + // Set foo project |
| 83 | + File fooDir = rootFolder.newFolder("SampleFooProject"); |
| 84 | + |
| 85 | + // Set mock for throw IOException (npm not installed) |
| 86 | + when(mockProcessHelper.runProcessBuilder(any(), any())).thenThrow( |
| 87 | + new IOException("Cannot run program \"npm\"")); |
| 88 | + NpmProjectType npmProjectType = new NpmProjectType(); |
| 89 | + |
| 90 | + npmProjectType.writeVersion(fooDir, Version.valueOf("1.1.0"), mockProcessHelper); |
| 91 | + } |
| 92 | +} |
0 commit comments