Search in sources :

Example 1 with FrontendVersion

use of com.vaadin.flow.server.frontend.FrontendVersion in project flow by vaadin.

the class NodeInstallerTest method installNodeFromFileSystem_NodeIsInstalledToTargetDirectory.

@Test
public void installNodeFromFileSystem_NodeIsInstalledToTargetDirectory() throws IOException {
    Platform platform = Platform.guess();
    String nodeExec = platform.isWindows() ? "node.exe" : "node";
    String prefix = String.format("node-%s-%s", FrontendTools.DEFAULT_NODE_VERSION, platform.getNodeClassifier(new FrontendVersion(FrontendTools.DEFAULT_NODE_VERSION)));
    File targetDir = new File(baseDir + "/installation");
    Assert.assertFalse("Clean test should not contain a installation folder", targetDir.exists());
    File downloadDir = tmpDir.newFolder(FrontendTools.DEFAULT_NODE_VERSION);
    File archiveFile = new File(downloadDir, prefix + "." + platform.getArchiveExtension());
    archiveFile.createNewFile();
    Path tempArchive = archiveFile.toPath();
    if (platform.getArchiveExtension().equals("zip")) {
        try (ZipOutputStream zipOutputStream = new ZipOutputStream(Files.newOutputStream(tempArchive))) {
            zipOutputStream.putNextEntry(new ZipEntry(prefix + "/" + nodeExec));
            zipOutputStream.closeEntry();
            zipOutputStream.putNextEntry(new ZipEntry(prefix + "/node_modules/npm/bin/npm"));
            zipOutputStream.closeEntry();
            zipOutputStream.putNextEntry(new ZipEntry(prefix + "/node_modules/npm/bin/npm.cmd"));
            zipOutputStream.closeEntry();
        }
    } else {
        try (OutputStream fo = Files.newOutputStream(tempArchive);
            OutputStream gzo = new GzipCompressorOutputStream(fo);
            ArchiveOutputStream o = new TarArchiveOutputStream(gzo)) {
            o.putArchiveEntry(o.createArchiveEntry(new File(prefix + "/bin/" + nodeExec), prefix + "/bin/" + nodeExec));
            o.closeArchiveEntry();
            o.putArchiveEntry(o.createArchiveEntry(new File(prefix + "/bin/npm"), prefix + "/bin/npm"));
            o.closeArchiveEntry();
            o.putArchiveEntry(o.createArchiveEntry(new File(prefix + "/lib/node_modules/npm/bin/npm"), prefix + "/lib/node_modules/npm/bin/npm"));
            o.closeArchiveEntry();
            o.putArchiveEntry(o.createArchiveEntry(new File(prefix + "/lib/node_modules/npm/bin/npm.cmd"), prefix + "/lib/node_modules/npm/bin/npm.cmd"));
            o.closeArchiveEntry();
        }
    }
    // add a file to node/node_modules_npm that should be cleaned out
    File nodeDirectory = new File(targetDir, "node");
    File nodeModulesDirectory = new File(nodeDirectory, "node_modules");
    File npmDirectory = new File(nodeModulesDirectory, "npm");
    File garbage = new File(npmDirectory, "garbage");
    FileUtils.forceMkdir(npmDirectory);
    Assert.assertTrue("garbage file should be created", garbage.createNewFile());
    NodeInstaller nodeInstaller = new NodeInstaller(targetDir, Collections.emptyList()).setNodeVersion(FrontendTools.DEFAULT_NODE_VERSION).setNodeDownloadRoot(new File(baseDir).toPath().toUri());
    try {
        nodeInstaller.install();
    } catch (InstallationException e) {
        throw new IllegalStateException("Failed to install Node", e);
    }
    Assert.assertTrue("npm should have been copied to node_modules", new File(targetDir, "node/" + nodeExec).exists());
    Assert.assertTrue("npm should have been copied to node_modules", new File(targetDir, "node/node_modules/npm/bin/npm").exists());
    Assert.assertFalse("old npm files should have been removed", garbage.exists());
}
Also used : Path(java.nio.file.Path) GzipCompressorOutputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream) ZipEntry(java.util.zip.ZipEntry) OutputStream(java.io.OutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) ArchiveOutputStream(org.apache.commons.compress.archivers.ArchiveOutputStream) GzipCompressorOutputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream) FrontendVersion(com.vaadin.flow.server.frontend.FrontendVersion) ZipOutputStream(java.util.zip.ZipOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) File(java.io.File) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) ArchiveOutputStream(org.apache.commons.compress.archivers.ArchiveOutputStream) Test(org.junit.Test)

Aggregations

FrontendVersion (com.vaadin.flow.server.frontend.FrontendVersion)1 File (java.io.File)1 OutputStream (java.io.OutputStream)1 Path (java.nio.file.Path)1 ZipEntry (java.util.zip.ZipEntry)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 ArchiveOutputStream (org.apache.commons.compress.archivers.ArchiveOutputStream)1 TarArchiveOutputStream (org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)1 GzipCompressorOutputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream)1 Test (org.junit.Test)1