public void GeneratedBuildSnippet_CustomBuildCommandWillExecute_WhenOtherCommandsAlsoExist() { // Arrange const string packageJson = @"{ ""main"": ""server.js"", ""scripts"": { }, }"; var commonOptions = new BuildScriptGeneratorOptions(); var nodePlatform = CreateNodePlatform( commonOptions, new NodeScriptGeneratorOptions { CustomBuildCommand = "custom build command", CustomRunBuildCommand = "custom run command" }, new NodePlatformInstaller( Options.Create(commonOptions), NullLoggerFactory.Instance)); var repo = new MemorySourceRepo(); repo.AddFile(packageJson, NodeConstants.PackageJsonFileName); var context = CreateContext(repo); var detectorResult = new NodePlatformDetectorResult { Platform = NodeConstants.PlatformName, PlatformVersion = "10.10", }; // Act var buildScriptSnippet = nodePlatform.GenerateBashBuildScriptSnippet(context, detectorResult); // Assert Assert.NotNull(buildScriptSnippet); Assert.Contains("custom build command", buildScriptSnippet.BashBuildScriptSnippet); Assert.DoesNotContain("custom run command", buildScriptSnippet.BashBuildScriptSnippet); }
public void GeneratedBuildSnippet_DoesNotThrowException_IfPackageJsonHasBuildAzureNode_AndRequireBuildPropertyIsSet() { // Arrange const string packageJson = @"{ ""main"": ""server.js"", ""scripts"": { ""build:azure"": ""azure-node"", }, }"; var commonOptions = new BuildScriptGeneratorOptions(); var nodePlatform = CreateNodePlatform( commonOptions, new NodeScriptGeneratorOptions(), new NodePlatformInstaller( Options.Create(commonOptions), NullLoggerFactory.Instance)); var repo = new MemorySourceRepo(); repo.AddFile(packageJson, NodeConstants.PackageJsonFileName); var context = CreateContext(repo); context.Properties[NodePlatform.RequireBuildPropertyKey] = "true"; var detectorResult = new NodePlatformDetectorResult { Platform = NodeConstants.PlatformName, PlatformVersion = "10.10", }; // Act var buildScriptSnippet = nodePlatform.GenerateBashBuildScriptSnippet(context, detectorResult); // Assert Assert.NotNull(buildScriptSnippet); Assert.Contains("npm run build:azure", buildScriptSnippet.BashBuildScriptSnippet); }
public void GeneratedBuildSnippet_HasNpmRunBuildAzureCommand() { // Arrange const string packageJson = @"{ ""main"": ""server.js"", ""scripts"": { ""build:azure"": ""build-azure-node"", }, }"; var commonOptions = new BuildScriptGeneratorOptions(); var nodePlatform = CreateNodePlatform( commonOptions, new NodeScriptGeneratorOptions { CustomRunBuildCommand = null }, new NodePlatformInstaller( Options.Create(commonOptions), NullLoggerFactory.Instance)); var repo = new MemorySourceRepo(); repo.AddFile(packageJson, NodeConstants.PackageJsonFileName); var context = CreateContext(repo); var detectorResult = new NodePlatformDetectorResult { Platform = NodeConstants.PlatformName, PlatformVersion = "10.10", }; // Act var buildScriptSnippet = nodePlatform.GenerateBashBuildScriptSnippet(context, detectorResult); // Assert Assert.NotNull(buildScriptSnippet); Assert.Contains("npm run build:azure", buildScriptSnippet.BashBuildScriptSnippet); }
public void GeneratedBuildSnippet_ThrowsException_IfBothLernaAndLageFileExists() { // Arrange var commonOptions = new BuildScriptGeneratorOptions(); var nodePlatform = CreateNodePlatform( commonOptions, new NodeScriptGeneratorOptions { CustomRunBuildCommand = null, EnableNodeMonorepoBuild = true }, new NodePlatformInstaller( Options.Create(commonOptions), NullLoggerFactory.Instance)); var repo = new MemorySourceRepo(); repo.AddFile(string.Empty, NodeConstants.PackageJsonFileName); repo.AddFile(string.Empty, NodeConstants.LageConfigJSFileName); repo.AddFile(string.Empty, NodeConstants.LernaJsonFileName); var context = CreateContext(repo); var detectorResult = new NodePlatformDetectorResult { Platform = NodeConstants.PlatformName, PlatformVersion = "10.10", HasLernaJsonFile = true, HasLageConfigJSFile = true, }; // Act & Assert var exception = Assert.Throws <InvalidUsageException>( () => nodePlatform.GenerateBashBuildScriptSnippet(context, detectorResult)); Assert.Equal( "Multiple monorepo package management tools are found, please choose to use either Lerna or Lage.", exception.Message); }
public void GeneratedScript_RunsNpmPackCommand_ExitWhenPackageDirectoryDoesNotExist() { // Arrange var scriptGenerator = GetNodePlatform( defaultNodeVersion: NodeVersions.Node12Version, new BuildScriptGeneratorOptions { PlatformVersion = "8.2.1", ShouldPackage = true }, new NodeScriptGeneratorOptions()); var repo = new MemorySourceRepo(); repo.AddFile(PackageJsonWithNpmVersion, NodeConstants.PackageJsonFileName); var context = CreateScriptGeneratorContext(repo); context.Properties[NodePlatform.PackageDirectoryPropertyKey] = "packages/random"; var detectorResult = new NodePlatformDetectorResult { Platform = NodeConstants.PlatformName, PlatformVersion = "10.10.10", }; // Act var snippet = scriptGenerator.GenerateBashBuildScriptSnippet(context, detectorResult); // Assert Assert.NotNull(snippet); Assert.Contains("Switching to package directory provided: " + $"'{context.Properties[NodePlatform.PackageDirectoryPropertyKey]}'...", snippet.BashBuildScriptSnippet); Assert.Contains($"Package directory '$SOURCE_DIR/$packageDirName' does not exist.", snippet.BashBuildScriptSnippet); }
public void GeneratedScript_WritesNpmRc_WithCustomRegistry() { // Arrange var scriptGenerator = GetNodePlatform( defaultNodeVersion: NodeVersions.Node12Version, new BuildScriptGeneratorOptions { PlatformVersion = "8.2.1" }, new NodeScriptGeneratorOptions()); var repo = new MemorySourceRepo(); repo.AddFile(PackageJsonWithNpmVersion, NodeConstants.PackageJsonFileName); var context = CreateScriptGeneratorContext(repo); context.Properties[NodePlatform.RegistryUrlPropertyKey] = "https://example.com/registry/"; var detectorResult = new NodePlatformDetectorResult { Platform = NodeConstants.PlatformName, PlatformVersion = "10.10.10", }; // Act var snippet = scriptGenerator.GenerateBashBuildScriptSnippet(context, detectorResult); // Assert Assert.NotNull(snippet); Assert.Contains( $"echo \"registry={context.Properties[NodePlatform.RegistryUrlPropertyKey]}\" >> ~/.npmrc", snippet.BashBuildScriptSnippet); }
public void GeneratedScript_ZipsNodeModules_IfZipNodeProperty_IsNull() { // Arrange var commonOptions = new BuildScriptGeneratorOptions(); commonOptions.PlatformVersion = "8.2.1"; commonOptions.Properties = new Dictionary <string, string>(); commonOptions.Properties[NodePlatform.CompressNodeModulesPropertyKey] = null; var scriptGenerator = GetNodePlatform( defaultNodeVersion: NodeVersions.Node12Version, commonOptions, new NodeScriptGeneratorOptions()); var repo = new MemorySourceRepo(); repo.AddFile(PackageJsonWithBuildScript, NodeConstants.PackageJsonFileName); var context = CreateScriptGeneratorContext(repo); context.Properties[NodePlatform.CompressNodeModulesPropertyKey] = null; var detectorResult = new NodePlatformDetectorResult { Platform = NodeConstants.PlatformName, PlatformVersion = "10.10.10", }; var expected = new NodeBashBuildSnippetProperties { PackageInstallCommand = NpmInstallCommand, PackageInstallerVersionCommand = NodeConstants.NpmVersionCommand, NpmRunBuildCommand = "npm run build", NpmRunBuildAzureCommand = "npm run build:azure", HasProdDependencies = true, HasDevDependencies = true, ProductionOnlyPackageInstallCommand = string.Format( NodeConstants.ProductionOnlyPackageInstallCommandTemplate, NpmInstallCommand), CompressedNodeModulesFileName = "node_modules.tar.gz", CompressNodeModulesCommand = "tar -zcf", NodeBuildProperties = new Dictionary <string, string> { { "PlatformWithVersion", "Node.js 10.10.10" }, }, NodeBuildCommandsFile = FilePaths.BuildCommandsFileName, }; // Act var snippet = scriptGenerator.GenerateBashBuildScriptSnippet(context, detectorResult); // Assert Assert.NotNull(snippet); Assert.Contains("echo Zipping existing 'node_modules' folder", snippet.BashBuildScriptSnippet); Assert.Equal( TemplateHelper.Render(TemplateHelper.TemplateResource.NodeBuildSnippet, expected), snippet.BashBuildScriptSnippet); Assert.True(scriptGenerator.IsCleanRepo(repo)); }
public void GeneratedScript_UsesYarn2InstallAndRunsNpmBuild_IfYarnRCIsPresent() { // Arrange var scriptGenerator = GetNodePlatform( defaultNodeVersion: NodeVersions.Node12Version, new BuildScriptGeneratorOptions { PlatformVersion = "8.2.1" }, new NodeScriptGeneratorOptions()); var repo = new MemorySourceRepo(); repo.AddFile(PackageJsonWithBuildScript, NodeConstants.PackageJsonFileName); repo.AddFile("", NodeConstants.YarnLockFileName); repo.AddFile("", ".yarnrc.yml"); var context = CreateScriptGeneratorContext(repo); var detectorResult = new NodePlatformDetectorResult { Platform = NodeConstants.PlatformName, PlatformVersion = "10.10.10", HasYarnrcYmlFile = true, IsYarnLockFileValidYamlFormat = true, }; var expected = new NodeBashBuildSnippetProperties { PackageInstallCommand = NodeConstants.Yarn2PackageInstallCommand, PackageInstallerVersionCommand = NodeConstants.YarnVersionCommand, NpmRunBuildCommand = "yarn run build", NpmRunBuildAzureCommand = "yarn run build:azure", HasProdDependencies = true, HasDevDependencies = true, ProductionOnlyPackageInstallCommand = string.Format( NodeConstants.ProductionOnlyPackageInstallCommandTemplate, NodeConstants.Yarn2PackageInstallCommand), CompressedNodeModulesFileName = null, CompressNodeModulesCommand = null, ConfigureYarnCache = false, NodeBuildProperties = new Dictionary <string, string> { { "PlatformWithVersion", "Node.js 10.10.10" }, }, NodeBuildCommandsFile = FilePaths.BuildCommandsFileName, }; // Act var snippet = scriptGenerator.GenerateBashBuildScriptSnippet(context, detectorResult); // Assert Assert.NotNull(snippet); Assert.Equal( TemplateHelper.Render(TemplateHelper.TemplateResource.NodeBuildSnippet, expected), snippet.BashBuildScriptSnippet); Assert.True(scriptGenerator.IsCleanRepo(repo)); }
public void GeneratedScript_DoesNotZipNodeModules_IfZipNodeModulesEnvironmentVariable_False() { // Arrange var scriptGenerator = GetNodePlatform( defaultNodeVersion: NodeVersions.Node12Version, new BuildScriptGeneratorOptions { PlatformVersion = "8.2.1" }, new NodeScriptGeneratorOptions()); var repo = new MemorySourceRepo(); repo.AddFile(PackageJsonWithBuildScript, NodeConstants.PackageJsonFileName); var context = CreateScriptGeneratorContext(repo); var detectorResult = new NodePlatformDetectorResult { Platform = NodeConstants.PlatformName, PlatformVersion = "10.10.10", }; //var commandManifestProperties = new Dictionary<string, string> //{ // {"PlatformWithVersion", "10.10.10" }, // }; var expected = new NodeBashBuildSnippetProperties { PackageInstallCommand = NpmInstallCommand, PackageInstallerVersionCommand = NodeConstants.NpmVersionCommand, NpmRunBuildCommand = "npm run build", NpmRunBuildAzureCommand = "npm run build:azure", HasProdDependencies = true, HasDevDependencies = true, ProductionOnlyPackageInstallCommand = string.Format( NodeConstants.ProductionOnlyPackageInstallCommandTemplate, NpmInstallCommand), CompressedNodeModulesFileName = null, CompressNodeModulesCommand = null, NodeBuildProperties = new Dictionary <string, string> { { "PlatformWithVersion", "Node.js 10.10.10" }, }, NodeBuildCommandsFile = FilePaths.BuildCommandsFileName, }; // Act var snippet = scriptGenerator.GenerateBashBuildScriptSnippet(context, detectorResult); // Assert Assert.NotNull(snippet); Assert.Equal( TemplateHelper.Render(TemplateHelper.TemplateResource.NodeBuildSnippet, expected), snippet.BashBuildScriptSnippet); Assert.True(scriptGenerator.IsCleanRepo(repo)); }
public void GeneratedScript_UsesYarnInstallAndRunsNpmBuild_IfYarnLockIsPresent_AndHasBuildNodeUnderScripts() { // Arrange var scriptGenerator = GetNodePlatform( defaultNodeVersion: NodeVersions.Node12Version, new BuildScriptGeneratorOptions { PlatformVersion = "8.2.1" }, new NodeScriptGeneratorOptions()); var repo = new MemorySourceRepo(); repo.AddFile(PackageJsonWithBuildScript, NodeConstants.PackageJsonFileName); repo.AddFile("Yarn lock file content here", NodeConstants.YarnLockFileName); var context = CreateScriptGeneratorContext(repo); var detectorResult = new NodePlatformDetectorResult { Platform = NodeConstants.PlatformName, PlatformVersion = "10.10.10", }; var expected = new NodeBashBuildSnippetProperties { PackageInstallCommand = YarnInstallCommand, PackageInstallerVersionCommand = NodeConstants.YarnVersionCommand, NpmRunBuildCommand = "yarn run build", NpmRunBuildAzureCommand = "yarn run build:azure", HasProdDependencies = true, HasDevDependencies = true, ProductionOnlyPackageInstallCommand = string.Format( NodeConstants.ProductionOnlyPackageInstallCommandTemplate, YarnInstallCommand), CompressedNodeModulesFileName = null, CompressNodeModulesCommand = null, ConfigureYarnCache = true, YarnCacheFolderName = "cache-folder", }; // Act var snippet = scriptGenerator.GenerateBashBuildScriptSnippet(context, detectorResult); // Assert Assert.NotNull(snippet); Assert.Equal( TemplateHelper.Render(TemplateHelper.TemplateResource.NodeBuildSnippet, expected), snippet.BashBuildScriptSnippet); Assert.True(scriptGenerator.IsCleanRepo(repo)); }
public void GeneratedScript_HasNpmVersion_SpecifiedInPackageJson() { // Arrange var scriptGenerator = GetNodePlatform( defaultNodeVersion: NodeVersions.Node12Version, new BuildScriptGeneratorOptions { PlatformVersion = "8.2.1" }, new NodeScriptGeneratorOptions()); var repo = new MemorySourceRepo(); repo.AddFile(PackageJsonWithNpmVersion, NodeConstants.PackageJsonFileName); var context = CreateScriptGeneratorContext(repo); var detectorResult = new NodePlatformDetectorResult { Platform = NodeConstants.PlatformName, PlatformVersion = "10.10.10", }; var expected = new NodeBashBuildSnippetProperties { PackageInstallCommand = NpmInstallCommand, PackageInstallerVersionCommand = NodeConstants.NpmVersionCommand, NpmRunBuildCommand = null, NpmRunBuildAzureCommand = null, HasProdDependencies = true, HasDevDependencies = false, ProductionOnlyPackageInstallCommand = string.Format( NodeConstants.ProductionOnlyPackageInstallCommandTemplate, NpmInstallCommand), CompressedNodeModulesFileName = null, CompressNodeModulesCommand = null, }; // Act var snippet = scriptGenerator.GenerateBashBuildScriptSnippet(context, detectorResult); // Assert Assert.NotNull(snippet); Assert.Equal( TemplateHelper.Render(TemplateHelper.TemplateResource.NodeBuildSnippet, expected), snippet.BashBuildScriptSnippet); Assert.True(scriptGenerator.IsCleanRepo(repo)); }
public void BuildScript_DoesNotHaveSdkInstallScript_IfDynamicInstallNotEnabled_AndSdkIsNotAlreadyInstalled() { // Arrange var nodePlatform = CreateNodePlatform(dynamicInstallIsEnabled: false, sdkAlreadyInstalled: false); var repo = new MemorySourceRepo(); repo.AddFile(string.Empty, NodeConstants.PackageJsonFileName); var context = CreateContext(repo); var detectorResult = new NodePlatformDetectorResult { Platform = NodeConstants.PlatformName, PlatformVersion = "10.10", }; // Act var buildScriptSnippet = nodePlatform.GetInstallerScriptSnippet(context, detectorResult); // Assert Assert.Null(buildScriptSnippet); }
private static PlatformDetectorResult GetPlatformDetectorResult(string name, string version) { var result = new PlatformDetectorResult(); switch (name) { case DotNetCoreConstants.PlatformName: result = new DotNetCorePlatformDetectorResult(); break; case NodeConstants.PlatformName: result = new NodePlatformDetectorResult(); break; case PythonConstants.PlatformName: result = new PythonPlatformDetectorResult(); break; case HugoConstants.PlatformName: result = new PlatformDetectorResult(); break; case PhpConstants.PlatformName: result = new PhpPlatformDetectorResult(); break; case JavaConstants.PlatformName: result = new JavaPlatformDetectorResult(); break; case RubyConstants.PlatformName: result = new RubyPlatformDetectorResult(); break; } result.Platform = name; result.PlatformVersion = version; return(result); }
public void GeneratedBuildSnippet_UsingYarn2Commands() { // Arrange const string packageJson = @"{ ""main"": ""server.js"", ""scripts"": { }, }"; var commonOptions = new BuildScriptGeneratorOptions(); var nodePlatform = CreateNodePlatform( commonOptions, new NodeScriptGeneratorOptions { CustomRunBuildCommand = null }, new NodePlatformInstaller( Options.Create(commonOptions), NullLoggerFactory.Instance)); var repo = new MemorySourceRepo(); repo.AddFile(packageJson, NodeConstants.PackageJsonFileName); repo.AddFile("", NodeConstants.YarnLockFileName); repo.AddFile("", ".yarnrc.yml"); var context = CreateContext(repo); var detectorResult = new NodePlatformDetectorResult { Platform = NodeConstants.PlatformName, PlatformVersion = "10.10", HasYarnrcYmlFile = true, IsYarnLockFileValidYamlFormat = true, }; // Act var buildScriptSnippet = nodePlatform.GenerateBashBuildScriptSnippet(context, detectorResult); // Assert Assert.NotNull(buildScriptSnippet); Assert.Contains("yarnCacheFolderName=cacheFolder", buildScriptSnippet.BashBuildScriptSnippet); Assert.Contains("yarn workspaces focus --all --production", buildScriptSnippet.BashBuildScriptSnippet); }
public void GeneratedBuildSnippet_ThrowsException_IfRequireBuildPropertyIsSet_AndNoBuildStepIsProvided() { // Arrange const string packageJson = @"{ ""main"": ""server.js"", ""scripts"": { }, }"; var commonOptions = new BuildScriptGeneratorOptions(); var nodePlatform = CreateNodePlatform( commonOptions, new NodeScriptGeneratorOptions(), new NodePlatformInstaller( Options.Create(commonOptions), NullLoggerFactory.Instance)); var repo = new MemorySourceRepo(); repo.AddFile(packageJson, NodeConstants.PackageJsonFileName); var context = CreateContext(repo); context.Properties[NodePlatform.RequireBuildPropertyKey] = "true"; var detectorResult = new NodePlatformDetectorResult { Platform = NodeConstants.PlatformName, PlatformVersion = "10.10", }; // Act & Assert var exception = Assert.Throws <NoBuildStepException>( () => nodePlatform.GenerateBashBuildScriptSnippet(context, detectorResult)); Assert.Equal( "Could not find either 'build' or 'build:azure' node under 'scripts' in package.json. " + "Could not find value for custom run build command using the environment variable " + "key 'RUN_BUILD_COMMAND'." + "Could not find tools for building monorepos, no 'lerna.json' or 'lage.config.js' files found.", exception.Message); }
public void GeneratedBuildSnippet_DoesNotThrowException_IfNoBuildStepIsProvided_AndRequireBuildPropertyIsSet_ToFalse() { // Arrange const string packageJson = @"{ ""main"": ""server.js"", ""scripts"": { }, }"; var commonOptions = new BuildScriptGeneratorOptions(); var nodePlatform = CreateNodePlatform( commonOptions, new NodeScriptGeneratorOptions { CustomRunBuildCommand = "custom command here" }, new NodePlatformInstaller( Options.Create(commonOptions), NullLoggerFactory.Instance)); var repo = new MemorySourceRepo(); repo.AddFile(packageJson, NodeConstants.PackageJsonFileName); var context = CreateContext(repo); context.Properties[NodePlatform.RequireBuildPropertyKey] = "false"; var detectorResult = new NodePlatformDetectorResult { Platform = NodeConstants.PlatformName, PlatformVersion = "10.10", }; // Act var buildScriptSnippet = nodePlatform.GenerateBashBuildScriptSnippet(context, detectorResult); // Assert Assert.NotNull(buildScriptSnippet); Assert.Contains("custom command here", buildScriptSnippet.BashBuildScriptSnippet); }
public void GeneratedBuildSnippet_UsesYarn_WhenEnginesFieldIsSet() { // Arrange const string packageJson = @"{ ""engines"": { ""yarn"": ""~1.22.11"" } }"; var commonOptions = new BuildScriptGeneratorOptions(); var nodePlatform = CreateNodePlatform( commonOptions, new NodeScriptGeneratorOptions { CustomRunBuildCommand = null }, new NodePlatformInstaller( Options.Create(commonOptions), NullLoggerFactory.Instance)); var repo = new MemorySourceRepo(); repo.AddFile(packageJson, NodeConstants.PackageJsonFileName); var context = CreateContext(repo); var detectorResult = new NodePlatformDetectorResult { Platform = NodeConstants.PlatformName, PlatformVersion = "10.10", HasYarnrcYmlFile = true, IsYarnLockFileValidYamlFormat = true, }; // Act var buildScriptSnippet = nodePlatform.GenerateBashBuildScriptSnippet(context, detectorResult); // Assert Assert.NotNull(buildScriptSnippet); Assert.Contains("yarn workspaces focus --all", buildScriptSnippet.BashBuildScriptSnippet); }
public void GeneratedBuildSnippet_WillNotBuildMonorepo_IfNodeMonorepoOptionNotEnabled() { // Arrange const string lernaJson = @"{ ""version"": ""3.22.1"", }"; var commonOptions = new BuildScriptGeneratorOptions(); var nodePlatform = CreateNodePlatform( commonOptions, new NodeScriptGeneratorOptions { CustomRunBuildCommand = null }, new NodePlatformInstaller( Options.Create(commonOptions), NullLoggerFactory.Instance)); var repo = new MemorySourceRepo(); repo.AddFile(string.Empty, NodeConstants.PackageJsonFileName); repo.AddFile(lernaJson, NodeConstants.LernaJsonFileName); var context = CreateContext(repo); var detectorResult = new NodePlatformDetectorResult { Platform = NodeConstants.PlatformName, PlatformVersion = "10.10", HasLernaJsonFile = true, LernaNpmClient = "npm", }; // Act var buildScriptSnippet = nodePlatform.GenerateBashBuildScriptSnippet(context, detectorResult); // Assert Assert.NotNull(buildScriptSnippet); Assert.DoesNotContain("lerna bootstrap", buildScriptSnippet.BashBuildScriptSnippet); Assert.DoesNotContain("lerna run build", buildScriptSnippet.BashBuildScriptSnippet); }