Skip to content

Commit

Permalink
Williamhe/1366864 fix issue 1073 yarn engine detection (microsoft#1140)
Browse files Browse the repository at this point in the history
* Add fix for microsoft#1073

* Add fix for microsoft#1073 unit test

* Update package manager to an invalid one

* Add E2E test

* Update engine type
  • Loading branch information
william-msft authored Oct 28, 2021
1 parent 6d49236 commit 369b946
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/BuildScriptGenerator/Node/NodePlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public BuildScriptSnippet GenerateBashBuildScriptSnippet(
throw new InvalidUsageException("Multiple monorepo package management tools are found, please choose to use either Lerna or Lage.");
}

if (ctx.SourceRepo.FileExists(NodeConstants.YarnLockFileName))
if (ctx.SourceRepo.FileExists(NodeConstants.YarnLockFileName) || packageJson?.engines?.yarn != null)
{
packageManagerCmd = NodeConstants.YarnCommand;
configureYarnCache = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class NodeBuildScriptGenerationTest
},
""author"": ""Dev"",
""license"": ""ISC"",
""engines"" : { ""node"" : ""6.11.0"" }
""engines"" : {""randomPackageManager"": ""0.0.0"" }
}";

private const string PackageJsonWithNpmVersion = @"{
Expand Down
35 changes: 35 additions & 0 deletions tests/BuildScriptGenerator.Tests/Node/NodePlatformTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,41 @@ public void GeneratedBuildSnippet_UsingYarn2Commands()
Assert.Contains("yarn workspaces focus --all", buildScriptSnippet.BashBuildScriptSnippet);
}

[Fact]
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);
}

[Fact]
public void BuildScript_HasSdkInstallScript_IfDynamicInstallIsEnabled_AndSdkIsNotAlreadyInstalled()
{
Expand Down
40 changes: 40 additions & 0 deletions tests/Oryx.BuildImage.Tests/Node/NodeJsSampleAppsOtherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,46 @@ public void CanBuildAppHavingAppDynamicsNpmPackage()
result.GetDebugInfo());
}

[Fact]
public void CanBuildAppHavingUsingYarnEngine()
{
// Arrange
// Create an app folder with a package.json having the yarn engine
var packageJsonContent = @"{
""engines"": {
""yarn"": ""~1.22.10""
}
}";
var sampleAppPath = Path.Combine(_tempRootDir, Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(sampleAppPath);
File.WriteAllText(Path.Combine(sampleAppPath, NodeConstants.PackageJsonFileName), packageJsonContent);
var volume = DockerVolume.CreateMirror(sampleAppPath);
var appDir = volume.ContainerDir;
var appOutputDir = "/tmp/output";
var script = new ShellScriptBuilder()
.AddBuildCommand($"{appDir} -i /tmp/int -o {appOutputDir}")
.AddDirectoryExistsCheck($"{appOutputDir}/node_modules")
.ToString();

// Act
var result = _dockerCli.Run(new DockerRunArguments
{
ImageId = Settings.LtsVersionsBuildImageName,
Volumes = new List<DockerVolume> { volume },
CommandToExecuteOnRun = "/bin/bash",
CommandArguments = new[] { "-c", script }
});

// Assert
RunAsserts(
() =>
{
Assert.True(result.IsSuccess);
Assert.Contains("Using Yarn version", result.StdOut);
},
result.GetDebugInfo());
}

[Fact]
public void CanBuildVuePressSampleAppWithPruneDevDependencies()
{
Expand Down

0 comments on commit 369b946

Please sign in to comment.