TaskRunNpmInstall implements FallibleCommand { @Override public void execute() throws ExecutionFailedException { String toolName = enablePnpm ? "pnpm" : "npm"; if (packageUpdater.modified || shouldRunNpmInstall()) { packageUpdater.log().info("Running `" + toolName + " install` to " + "resolve and optionally download frontend dependencies. " + "This may take a moment, please stand by..."); runNpmInstall(); updateLocalHash(); } else { packageUpdater.log().info( "Skipping `{} install` because the frontend packages are already " + "installed in the folder '{}' and the hash in the file '{}' is the same as in '{}'", toolName, packageUpdater.nodeModulesFolder.getAbsolutePath(), getLocalHashFile().getAbsolutePath(), Constants.PACKAGE_JSON); } } TaskRunNpmInstall(ClassFinder classFinder, NodeUpdater packageUpdater,
            boolean enablePnpm, boolean requireHomeNodeExec, String nodeVersion,
            URI nodeDownloadRoot); @Override void execute();  }
@Test public void runNpmInstall_emptyDir_npmInstallIsExecuted() throws ExecutionFailedException { File nodeModules = getNodeUpdater().nodeModulesFolder; nodeModules.mkdir(); nodeUpdater.modified = false; task.execute(); Mockito.verify(logger).info(getRunningMsg()); }
@Test public void runNpmInstall_nodeModulesContainsStaging_npmInstallIsExecuted() throws ExecutionFailedException { File nodeModules = getNodeUpdater().nodeModulesFolder; nodeModules.mkdir(); File staging = new File(nodeModules, ".staging"); staging.mkdir(); nodeUpdater.modified = false; task.execute(); Mockito.verify(logger).info(getRunningMsg()); }
@Test public void runNpmInstall_toolIsChanged_nodeModulesIsRemoved() throws ExecutionFailedException, IOException { File nodeModules = getNodeUpdater().nodeModulesFolder; nodeModules.mkdir(); nodeUpdater.modified = true; File yaml = new File(nodeModules, ".modules.yaml"); yaml.createNewFile(); task.execute(); Assert.assertFalse(yaml.exists()); }
@Test public void runNpmInstall_toolIsNotChanged_nodeModulesIsNotRemoved() throws ExecutionFailedException, IOException { File nodeModules = getNodeUpdater().nodeModulesFolder; nodeModules.mkdir(); nodeUpdater.modified = true; File fakeFile = new File(nodeModules, ".fake.file"); fakeFile.createNewFile(); task.execute(); Assert.assertTrue(fakeFile.exists()); }
@Test public void runNpmInstall_nonEmptyDirNoLocalHash_npmInstallIsExecuted() throws IOException, ExecutionFailedException { File nodeModules = getNodeUpdater().nodeModulesFolder; nodeModules.mkdir(); new File(nodeModules, "foo").createNewFile(); nodeUpdater.modified = false; task.execute(); Mockito.verify(logger).info(getRunningMsg()); }
@Test public void runNpmInstall_nonEmptyDirNoHashMatch_npmInstallIsExecuted() throws IOException, ExecutionFailedException { File nodeModules = getNodeUpdater().nodeModulesFolder; nodeModules.mkdir(); new File(nodeModules, "foo").createNewFile(); writeLocalHash("faulty"); nodeUpdater.modified = false; task.execute(); Mockito.verify(logger).info(getRunningMsg()); }
@Test public void runNpmInstall_matchingHash_npmInstallIsNotExecuted() throws IOException, ExecutionFailedException { File nodeModules = getNodeUpdater().nodeModulesFolder; nodeModules.mkdir(); new File(nodeModules, "foo").createNewFile(); writeLocalHash(""); nodeUpdater.modified = false; task.execute(); ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); Mockito.verify(logger).info(captor.capture(), Mockito.matches(getToolName()), Mockito.matches(nodeModules.getAbsolutePath()), Mockito.any(), Mockito.matches(Constants.PACKAGE_JSON)); Assert.assertEquals( "Skipping `{} install` because the frontend packages are already installed in the folder '{}' and the hash in the file '{}' is the same as in '{}'", captor.getValue()); }
@Test public void runNpmInstall_matchingHashButEmptyModules_npmInstallIsExecuted() throws IOException, ExecutionFailedException { File nodeModules = getNodeUpdater().nodeModulesFolder; nodeModules.mkdir(); writeLocalHash(""); nodeUpdater.modified = false; task.execute(); Mockito.verify(logger).info(getRunningMsg()); }
@Test public void runNpmInstall_dirContainsOnlyFlowNpmPackage_npmInstallIsNotExecuted() throws ExecutionFailedException { File nodeModules = getNodeUpdater().nodeModulesFolder; nodeModules.mkdir(); new File(nodeModules, "@vaadin/flow-frontend/").mkdirs(); nodeUpdater.modified = false; task.execute(); Mockito.verify(logger).info(getRunningMsg()); }
@Test public void runNpmInstall_modified_npmInstallIsExecuted() throws ExecutionFailedException { nodeUpdater.modified = true; task.execute(); Mockito.verify(logger).info(getRunningMsg()); }
@Test public void runNpmInstall_externalUpdateOfPackages_npmInstallIsRerun() throws ExecutionFailedException, IOException { nodeUpdater.modified = true; JsonObject packageJson = getNodeUpdater().getPackageJson(); updatePackageHash(packageJson); getNodeUpdater().writePackageFile(packageJson); task.execute(); final File localHashFile = new File(getNodeUpdater().nodeModulesFolder, ".vaadin/vaadin.json"); Assert.assertTrue("Local has file was not created after install.", localHashFile.exists()); String fileContent = FileUtils.readFileToString(localHashFile, UTF_8.name()); JsonObject localHash = Json.parse(fileContent); Assert.assertNotEquals("We should have a non empty hash key", "", localHash.getString(HASH_KEY)); packageJson = getNodeUpdater().getPackageJson(); packageJson.getObject(VAADIN_DEP_KEY).getObject(DEPENDENCIES) .put("a-avataaar", "^1.2.5"); String hash = packageJson.getObject(VAADIN_DEP_KEY).getString(HASH_KEY); updatePackageHash(packageJson); Assert.assertNotEquals("Hash should have been updated", hash, packageJson.getObject(VAADIN_DEP_KEY).getString(HASH_KEY)); getNodeUpdater().writePackageFile(packageJson); logger = Mockito.mock(Logger.class); task.execute(); Mockito.verify(logger).info(getRunningMsg()); }