Skip to content

Commit

Permalink
Make unit tests more reliable
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeRobich committed Jun 25, 2020
1 parent 67a3428 commit c0ca2c6
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 30 deletions.
3 changes: 1 addition & 2 deletions .vscode/launch.json
Expand Up @@ -52,8 +52,7 @@
"--fix-style",
"-v",
"diag",
"--check",
"-?"
"--check"
],
"cwd": "${workspaceFolder}",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
Expand Down
12 changes: 11 additions & 1 deletion src/MSBuild/LooseVersionAssemblyLoader.cs
Expand Up @@ -63,7 +63,17 @@ public static void Register(string searchPath)
continue;
}

return LoadAndCache(context, candidatePath);
try
{
return LoadAndCache(context, candidatePath);
}
catch
{
// We were unable to load the assembly from the file path. It is likely that
// a different version of the assembly has already been loaded into the context.
// Be forgiving and attempt to load assembly by name without specifying a version.
return context.LoadFromAssemblyName(new AssemblyName(assemblyName.Name));
}
}
}

Expand Down
23 changes: 11 additions & 12 deletions tests/CodeFormatterTests.cs
Expand Up @@ -15,18 +15,18 @@

namespace Microsoft.CodeAnalysis.Tools.Tests
{
public class CodeFormatterTests : IClassFixture<MSBuildFixture>, IClassFixture<SolutionPathFixture>
public class CodeFormatterTests : IClassFixture<MSBuildFixture>, IClassFixture<TestProjectsPathFixture>
{
private const string FormattedProjectPath = "tests/projects/for_code_formatter/formatted_project/";
private const string FormattedProjectPath = "for_code_formatter/formatted_project/";
private const string FormattedProjectFilePath = FormattedProjectPath + "formatted_project.csproj";
private const string FormattedSolutionFilePath = "tests/projects/for_code_formatter/formatted_solution/formatted_solution.sln";
private const string FormattedSolutionFilePath = "for_code_formatter/formatted_solution/formatted_solution.sln";

private const string UnformattedProjectPath = "tests/projects/for_code_formatter/unformatted_project/";
private const string UnformattedProjectPath = "for_code_formatter/unformatted_project/";
private const string UnformattedProjectFilePath = UnformattedProjectPath + "unformatted_project.csproj";
private const string UnformattedProgramFilePath = UnformattedProjectPath + "program.cs";
private const string UnformattedSolutionFilePath = "tests/projects/for_code_formatter/unformatted_solution/unformatted_solution.sln";
private const string UnformattedSolutionFilePath = "for_code_formatter/unformatted_solution/unformatted_solution.sln";

private const string FSharpProjectPath = "tests/projects/for_code_formatter/fsharp_project/";
private const string FSharpProjectPath = "for_code_formatter/fsharp_project/";
private const string FSharpProjectFilePath = FSharpProjectPath + "fsharp_project.fsproj";

private static IEnumerable<string> EmptyFilesList => Array.Empty<string>();
Expand All @@ -35,12 +35,11 @@ public class CodeFormatterTests : IClassFixture<MSBuildFixture>, IClassFixture<S

private readonly ITestOutputHelper _output;


public CodeFormatterTests(ITestOutputHelper output, MSBuildFixture msBuildFixture, SolutionPathFixture solutionPathFixture)
public CodeFormatterTests(ITestOutputHelper output, MSBuildFixture msBuildFixture, TestProjectsPathFixture testProjectsPathFixture)
{
_output = output;

solutionPathFixture.SetCurrentDirectory();
testProjectsPathFixture.SetCurrentDirectory();
msBuildFixture.RegisterInstance(_output);
}

Expand Down Expand Up @@ -96,8 +95,8 @@ public async Task GeneratedFilesFormattedInUnformattedProject()
expectedFileCount: 5);

var logLines = log.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
Assert.Contains(logLines, line => line.Contains("unformatted_project.AssemblyInfo.cs(1,1): Fix file encoding."));
Assert.Contains(logLines, line => line.Contains("NETCoreApp,Version=v3.0.AssemblyAttributes.cs(1,1): Fix file encoding."));
Assert.Contains(logLines, line => line.Contains("unformatted_project.AssemblyInfo.cs"));
Assert.Contains(logLines, line => line.Contains("NETCoreApp,Version=v3.0.AssemblyAttributes.cs"));
}

[Fact]
Expand All @@ -124,7 +123,7 @@ public async Task FilesFormattedInUnformattedProjectFolder()
includeGenerated: false,
expectedExitCode: 0,
expectedFilesFormatted: 2,
expectedFileCount: 3);
expectedFileCount: 5);
}

[Fact]
Expand Down
22 changes: 11 additions & 11 deletions tests/MSBuild/MSBuildWorkspaceFinderTests.cs
Expand Up @@ -8,19 +8,19 @@

namespace Microsoft.CodeAnalysis.Tools.Tests.MSBuild
{
public class MSBuildWorkspaceFinderTests : IClassFixture<SolutionPathFixture>
public class MSBuildWorkspaceFinderTests : IClassFixture<TestProjectsPathFixture>
{
private string SolutionPath => Environment.CurrentDirectory;

public MSBuildWorkspaceFinderTests(SolutionPathFixture solutionPathFixture)
public MSBuildWorkspaceFinderTests(TestProjectsPathFixture testProjectsPathFixture)
{
solutionPathFixture.SetCurrentDirectory();
testProjectsPathFixture.SetCurrentDirectory();
}

[Fact]
public void ThrowsException_CannotFindMSBuildProjectFile()
{
var workspacePath = "tests/projects/for_workspace_finder/no_project_or_solution/";
var workspacePath = "for_workspace_finder/no_project_or_solution/";
var exceptionMessageStart = string.Format(
Resources.Could_not_find_a_MSBuild_project_or_solution_file_in_0_Specify_which_to_use_with_the_workspace_argument,
Path.Combine(SolutionPath, workspacePath)).Replace('/', Path.DirectorySeparatorChar);
Expand All @@ -31,7 +31,7 @@ public void ThrowsException_CannotFindMSBuildProjectFile()
[Fact]
public void ThrowsException_MultipleMSBuildProjectFiles()
{
var workspacePath = "tests/projects/for_workspace_finder/multiple_projects/";
var workspacePath = "for_workspace_finder/multiple_projects/";
var exceptionMessageStart = string.Format(
Resources.Multiple_MSBuild_project_files_found_in_0_Specify_which_to_use_with_the_workspace_argument,
Path.Combine(SolutionPath, workspacePath)).Replace('/', Path.DirectorySeparatorChar);
Expand All @@ -42,7 +42,7 @@ public void ThrowsException_MultipleMSBuildProjectFiles()
[Fact]
public void ThrowsException_MultipleMSBuildSolutionFiles()
{
var workspacePath = "tests/projects/for_workspace_finder/multiple_solutions/";
var workspacePath = "for_workspace_finder/multiple_solutions/";
var exceptionMessageStart = string.Format(
Resources.Multiple_MSBuild_solution_files_found_in_0_Specify_which_to_use_with_the_workspace_argument,
Path.Combine(SolutionPath, workspacePath)).Replace('/', Path.DirectorySeparatorChar);
Expand All @@ -53,7 +53,7 @@ public void ThrowsException_MultipleMSBuildSolutionFiles()
[Fact]
public void ThrowsException_SolutionAndProjectAmbiguity()
{
var workspacePath = "tests/projects/for_workspace_finder/project_and_solution/";
var workspacePath = "for_workspace_finder/project_and_solution/";
var exceptionMessageStart = string.Format(
Resources.Both_a_MSBuild_project_file_and_solution_file_found_in_0_Specify_which_to_use_with_the_workspace_argument,
Path.Combine(SolutionPath, workspacePath)).Replace('/', Path.DirectorySeparatorChar);
Expand All @@ -64,7 +64,7 @@ public void ThrowsException_SolutionAndProjectAmbiguity()
[Fact]
public void FindsSolutionByFolder()
{
const string Path = "tests/projects/for_workspace_finder/single_solution/";
const string Path = "for_workspace_finder/single_solution/";

var (isSolution, workspacePath) = MSBuildWorkspaceFinder.FindWorkspace(SolutionPath, Path);

Expand All @@ -76,7 +76,7 @@ public void FindsSolutionByFolder()
[Fact]
public void FindsSolutionByFilePath()
{
const string Path = "tests/projects/for_workspace_finder/multiple_solutions/solution_b.sln";
const string Path = "for_workspace_finder/multiple_solutions/solution_b.sln";

var (isSolution, workspacePath) = MSBuildWorkspaceFinder.FindWorkspace(SolutionPath, Path);

Expand All @@ -88,7 +88,7 @@ public void FindsSolutionByFilePath()
[Fact]
public void FindsProjectByFolder()
{
const string Path = "tests/projects/for_workspace_finder/single_project/";
const string Path = "for_workspace_finder/single_project/";

var (isSolution, workspacePath) = MSBuildWorkspaceFinder.FindWorkspace(SolutionPath, Path);

Expand All @@ -100,7 +100,7 @@ public void FindsProjectByFolder()
[Fact]
public void FindsProjectByFilePath()
{
const string Path = "tests/projects/for_workspace_finder/multiple_projects/project_b.csproj";
const string Path = "for_workspace_finder/multiple_projects/project_b.csproj";

var (isSolution, workspacePath) = MSBuildWorkspaceFinder.FindWorkspace(SolutionPath, Path);

Expand Down
Expand Up @@ -7,9 +7,9 @@
namespace Microsoft.CodeAnalysis.Tools.Tests.Utilities
{
/// <summary>
/// This test fixture sets the <see cref="Environment.CurrentDirectory" /> to the dotnet-format solution's path.
/// This test fixture sets the <see cref="Environment.CurrentDirectory" /> to the dotnet-format test projects folder path.
/// </summary>
public class SolutionPathFixture : IDisposable
public class TestProjectsPathFixture : IDisposable
{
private static int s_registered = 0;
private static string s_currentDirectory;
Expand All @@ -20,7 +20,7 @@ public void SetCurrentDirectory()
{
s_currentDirectory = Environment.CurrentDirectory;
var solutionPath = Directory.GetParent(s_currentDirectory).Parent.Parent.Parent.Parent.FullName;
Environment.CurrentDirectory = solutionPath;
Environment.CurrentDirectory = Path.Combine(solutionPath, "tests", "projects");
}
}

Expand Down
4 changes: 4 additions & 0 deletions tests/projects/Directory.Build.props
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>
<!-- Empty props files to stop test projects from inheriting the root props file. -->
</Project>
4 changes: 4 additions & 0 deletions tests/projects/Directory.Build.targets
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>
<!-- Empty target files to stop test projects from inheriting the root targets file. -->
</Project>
Expand Up @@ -2,7 +2,7 @@
root = true

# C# files
[{Program.cs,other_items/*.cs}]
[{Program.cs,other_items/*.cs,obj/**/*.cs}]

#### Core EditorConfig Options ####

Expand Down

0 comments on commit c0ca2c6

Please sign in to comment.