Skip to content
This repository has been archived by the owner on Dec 18, 2017. It is now read-only.

Commit

Permalink
Update DnuListTests and AssemblyWalker to use new C# feature
Browse files Browse the repository at this point in the history
  • Loading branch information
troydai committed Apr 15, 2015
1 parent 04bd59f commit f9479ce
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 50 deletions.
6 changes: 3 additions & 3 deletions src/Microsoft.Framework.PackageManager/List/AssemblyWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ public void Walk(IGraphNode<LibraryDescription> root)
HashSet<string> packagesSources;
if (_dependencyPackageSources.TryGetValue(assemblyName, out packagesSources) && packagesSources.Any())
{
_reports.Information.WriteLine(" from package: {0}", string.Join(", ", packagesSources.ToArray()));
_reports.Information.WriteLine(" from package: {0}", string.Join(", ", packagesSources));
}

HashSet<string> assemblySources;
if (_dependencyAssemblySources.TryGetValue(assemblyName, out assemblySources) && assemblySources.Any())
{
_reports.Information.WriteLine(" from assembly: {0}", string.Join(", ", assemblySources.ToArray()));
_reports.Information.WriteLine(" from assembly: {0}", string.Join(", ", assemblySources));
}
}
}
Expand All @@ -86,7 +86,7 @@ private bool VisitLibrary(IGraphNode<LibraryDescription> node,

DepthFirstGraphTraversal.PreOrderWalk(
root: loadableAssembly,
visitNode: (assemblyName, assemblyAncestors) => VisitAssembly(assemblyName, assemblyAncestors),
visitNode: VisitAssembly,
getChildren: GetAssemblyDependencies);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ private void CreateNewPackage(string name, string version)
var outputDir = dir.CreateSubdirectory("output");
var projectJson = Path.Combine(projectDir.FullName, "project.json");

File.WriteAllText(projectJson, @"{
""version"": ""_version_""
}".Replace("_version_", version));
File.WriteAllText(projectJson, "{\"version\": \"" + version + "\"}");
DnuTestUtils.ExecDnu(runtimeHomePath, "pack", projectJson + " --out " + outputDir.FullName, environment: null, workingDir: null);

var packageName = string.Format("{0}.{1}.nupkg", name, version);
Expand Down
145 changes: 101 additions & 44 deletions test/Microsoft.Framework.PackageManager.FunctionalTests/DnuListTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.IO;
using System.Linq;
using Microsoft.Framework.FunctionalTestUtils;
using Newtonsoft.Json;
using Xunit;

namespace Microsoft.Framework.PackageManager.FunctionalTests
Expand Down Expand Up @@ -35,47 +36,71 @@ public void Dispose()
[MemberData("RuntimeComponents")]
public void DnuList_EmptyProject_Default(string flavor, string os, string architecture)
{
string stdOut, stdErr;
var runtimeHomePath = _context.GetRuntimeHome(flavor, os, architecture);
var expectedTitle = string.Format(
@"Listing dependencies for {0} ({1})",
Path.GetFileName(_workingDir.DirPath),
Path.Combine(_workingDir, "project.json"));

var projectJson = Path.Combine(_workingDir.DirPath, "project.json");
File.WriteAllText(projectJson, @"{}");
CreateProjectJson(@"{}");

string stdOut, stdErr;
// run dnu list
Assert.Equal(0, DnuTestUtils.ExecDnu(runtimeHomePath, "list", "", out stdOut, out stdErr, environment: null, workingDir: _workingDir.DirPath));

// assert
Assert.True(string.IsNullOrEmpty(stdErr));
Assert.True(stdOut.Contains(expectedTitle));
}

[Theory]
[MemberData("RuntimeComponents")]
public void DnuList_EmptyProject_Details(string flavor, string os, string architecture)
{
string stdOut, stdErr;
var runtimeHomePath = _context.GetRuntimeHome(flavor, os, architecture);
var projectJson = Path.Combine(_workingDir.DirPath, "project.json");
File.WriteAllText(projectJson, @"{}");
var expectedTitle = string.Format(
@"Listing dependencies for {0} ({1})",
Path.GetFileName(_workingDir.DirPath),
Path.Combine(_workingDir, "project.json"));

Assert.Equal(0, DnuTestUtils.ExecDnu(runtimeHomePath, "list", "--details", environment: null, workingDir: _workingDir.DirPath));
CreateProjectJson(@"{}");

// run dnu list
Assert.Equal(0, DnuTestUtils.ExecDnu(runtimeHomePath, "list", "--details", out stdOut, out stdErr, environment: null, workingDir: _workingDir.DirPath));

// assert
Assert.True(string.IsNullOrEmpty(stdErr));
Assert.True(stdOut.Contains(expectedTitle));
}

[Theory]
[MemberData("RuntimeComponents")]
public void DnuList_SingleDependencyProject(string flavor, string os, string architecture)
{
string stdOut, stdErr;
var runtimeHomePath = _context.GetRuntimeHome(flavor, os, architecture);
var projectJson = Path.Combine(_workingDir.DirPath, "project.json");
File.WriteAllText(projectJson, @"{
""dependencies"": {
""alpha"": ""0.1.0""
},
""frameworks"": {
""dnx451"": {},
""dnxcore50"": {}
}
}");

CreateProjectJson(new
{
dependencies = new
{
alpha = "0.1.0"
},
frameworks = new
{
dnx451 = new { },
dnxcore50 = new { }
}
});

// restore the packages
Assert.Equal(0, DnuTestUtils.ExecDnu(runtimeHomePath, "restore", "--source " + _context.PackageSource, workingDir: _workingDir.DirPath));

string stdOut, stdErr;
// run dnu list
Assert.Equal(0, DnuTestUtils.ExecDnu(runtimeHomePath, "list", "", out stdOut, out stdErr, environment: null, workingDir: _workingDir.DirPath));

// there should be 2 and only 2 dependencies of alpha
var hits = stdOut.Split('\n').Where(line => line.Contains("* alpha 0.1.0"))
.Where(line => !line.Contains("Unresolved"));
Assert.Equal(2, hits.Count());
Expand All @@ -85,33 +110,41 @@ public void DnuList_SingleDependencyProject(string flavor, string os, string arc
[MemberData("RuntimeComponents")]
public void DnuList_SingleDependencyProject_Detailed(string flavor, string os, string architecture)
{
string stdOut, stdErr;
var runtimeHomePath = _context.GetRuntimeHome(flavor, os, architecture);
var projectJson = Path.Combine(_workingDir.DirPath, "project.json");
File.WriteAllText(projectJson, @"{
""dependencies"": {
""alpha"": ""0.1.0""
},
""frameworks"": {
""dnx451"": {},
""dnxcore50"": {}
}
}");

var projectName = Path.GetFileName(_workingDir.DirPath);

CreateProjectJson(new
{
dependencies = new
{
alpha = "0.1.0"
},
frameworks = new
{
dnx451 = new { },
dnxcore50 = new { }
}
});

// restore the packages
Assert.Equal(0, DnuTestUtils.ExecDnu(runtimeHomePath, "restore", "--source " + _context.PackageSource, workingDir: _workingDir.DirPath));

string stdOut, stdErr;
// run dnu list
Assert.Equal(0, DnuTestUtils.ExecDnu(runtimeHomePath, "list", "--details", out stdOut, out stdErr, environment: null, workingDir: _workingDir.DirPath));

// assert - in the output of the dnu list, alpha 0.1.0 is listed as resolved, its source is listed on the second line.
string[] outputLines = stdOut.Split(Environment.NewLine[0]);

Assert.True(outputLines.Length > 0);
for (int i = 0; i < outputLines.Length; ++i)
{
if (outputLines[i].Contains("* alpha 0.1.0"))
{
Assert.False(outputLines[i].Contains("Unresolved"), "Dnu list reports unresolved package");
Assert.True(outputLines[i + 1].Contains(projectName));

// the following line should list the dependency's source
Assert.True(++i < outputLines.Length);
Assert.True(outputLines[i].Contains(projectName));
}
}
}
Expand All @@ -120,25 +153,49 @@ public void DnuList_SingleDependencyProject_Detailed(string flavor, string os, s
[MemberData("RuntimeComponents")]
public void DnuList_Unresolved(string flavor, string os, string architecture)
{
string stdOut, stdErr;
var runtimeHomePath = _context.GetRuntimeHome(flavor, os, architecture);
var projectJson = Path.Combine(_workingDir.DirPath, "project.json");
File.WriteAllText(projectJson, @"{
""dependencies"": {
""alpha"": ""0.1.0""
},
""frameworks"": {
""dnx451"": {},
""dnxcore50"": {}
}
}");

var projectName = Path.GetFileName(_workingDir.DirPath);

string stdOut, stdErr;
CreateProjectJson(new
{
dependencies = new
{
alpha = "0.1.0",
beta = "0.2.0"
},
frameworks = new
{
dnx451 = new { },
dnxcore50 = new { }
}
});

// restore the packages, it should fail because missing package beta
Assert.Equal(1, DnuTestUtils.ExecDnu(runtimeHomePath, "restore", "--source " + _context.PackageSource, workingDir: _workingDir.DirPath));

// run dnu list
Assert.Equal(0, DnuTestUtils.ExecDnu(runtimeHomePath, "list", "", out stdOut, out stdErr, environment: null, workingDir: _workingDir.DirPath));

var hits = stdOut.Split('\n').Where(line => line.Contains("* alpha 0.1.0") && line.Contains("Unresolved"));
// the beta package is not resolved
var hits = SplitLines(stdOut).Where(line => line.Contains("* beta 0.2.0 - Unresolved"));
Assert.Equal(2, hits.Count());
}

private string[] SplitLines(string content)
{
return content.Split(Environment.NewLine[0]);
}

private void CreateProjectJson(string content)
{
var projectJson = Path.Combine(_workingDir.DirPath, "project.json");
File.WriteAllText(projectJson, content);
}

private void CreateProjectJson(object content)
{
CreateProjectJson(JsonConvert.SerializeObject(content));
}
}
}

0 comments on commit f9479ce

Please sign in to comment.