Skip to content

Commit

Permalink
Merge pull request #18046 from jasonmalinowski/create-test-for-comman…
Browse files Browse the repository at this point in the history
…dline-build

Create test for commandline build
  • Loading branch information
jasonmalinowski authored Mar 22, 2017
2 parents e9c846c + 1b336c0 commit 3d21157
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 33 deletions.
1 change: 1 addition & 0 deletions src/EditorFeatures/TestUtilities/Traits.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public static class Features
public const string BlockCommentEditing = nameof(BlockCommentEditing);
public const string BraceHighlighting = nameof(BraceHighlighting);
public const string BraceMatching = nameof(BraceMatching);
public const string Build = nameof(Build);
public const string CallHierarchy = nameof(CallHierarchy);
public const string CaseCorrection = nameof(CaseCorrection);
public const string ChangeSignature = nameof(ChangeSignature);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Diagnostics;
using System.IO;
using Microsoft.CodeAnalysis;
using Microsoft.VisualStudio.IntegrationTest.Utilities;
using Roslyn.Test.Utilities;
using Xunit;

namespace Roslyn.VisualStudio.IntegrationTests.CSharp
{
[Collection(nameof(SharedIntegrationHostFixture))]
public class CSharpBuild : IDisposable
public class CSharpBuild : AbstractIntegrationTest
{
private readonly VisualStudioInstanceContext _visualStudio;

public CSharpBuild(VisualStudioInstanceFactory instanceFactory)
: base(instanceFactory)
{
_visualStudio = instanceFactory.GetNewOrUsedInstance(SharedIntegrationHostFixture.RequiredPackageIds);

_visualStudio.Instance.SolutionExplorer.CreateSolution(nameof(CSharpBuild));
_visualStudio.Instance.SolutionExplorer.AddProject("TestProj", WellKnownProjectTemplates.ConsoleApplication, LanguageNames.CSharp);
VisualStudio.Instance.SolutionExplorer.CreateSolution(nameof(CSharpBuild));
VisualStudio.Instance.SolutionExplorer.AddProject("TestProj", WellKnownProjectTemplates.ConsoleApplication, LanguageNames.CSharp);
}

public void Dispose()
{
_visualStudio.Dispose();
}

[Fact]
[Fact, Trait(Traits.Feature, Traits.Features.Build)]
public void BuildProject()
{
var editorText = @"using System;
Expand All @@ -38,10 +33,30 @@ static void Main(string[] args)
}
}";

_visualStudio.Instance.Editor.SetText(editorText);
VisualStudio.Instance.Editor.SetText(editorText);

// TODO: Validate build works as expected
}

[Fact, Trait(Traits.Feature, Traits.Features.Build)]
public void BuildWithCommandLine()
{
VisualStudio.Instance.SolutionExplorer.SaveAll();

var pathToDevenv = Path.Combine(VisualStudio.Instance.InstallationPath, @"Common7\IDE\devenv.exe");
var pathToSolution = VisualStudio.Instance.SolutionExplorer.SolutionFileFullPath;
var logFileName = pathToSolution + ".log";

File.Delete(logFileName);

var commandLine = $"\"{pathToSolution}\" /Rebuild Debug /Out \"{logFileName}\" {VisualStudioInstanceFactory.VsLaunchArgs}";

var process = Process.Start(pathToDevenv, commandLine);
process.WaitForExit();

Assert.Equal(0, process.ExitCode);

Assert.Contains("Rebuild All: 1 succeeded, 0 failed, 0 skipped", File.ReadAllText(logFileName));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,22 @@

using Microsoft.CodeAnalysis;
using Microsoft.VisualStudio.IntegrationTest.Utilities;
using Roslyn.Test.Utilities;
using Xunit;

namespace Roslyn.VisualStudio.IntegrationTests.VisualBasic
{
[Collection(nameof(SharedIntegrationHostFixture))]
public class BasicBuild
public class BasicBuild : AbstractIntegrationTest
{
private readonly VisualStudioInstanceContext _visualStudio;

public BasicBuild(VisualStudioInstanceFactory instanceFactory)
: base(instanceFactory)
{
_visualStudio = instanceFactory.GetNewOrUsedInstance(SharedIntegrationHostFixture.RequiredPackageIds);

_visualStudio.Instance.SolutionExplorer.CreateSolution(nameof(BasicBuild));
_visualStudio.Instance.SolutionExplorer.AddProject("TestProj", WellKnownProjectTemplates.ConsoleApplication, LanguageNames.VisualBasic);
VisualStudio.Instance.SolutionExplorer.CreateSolution(nameof(BasicBuild));
VisualStudio.Instance.SolutionExplorer.AddProject("TestProj", WellKnownProjectTemplates.ConsoleApplication, LanguageNames.VisualBasic);
}

[Fact]
[Fact, Trait(Traits.Feature, Traits.Features.Build)]
public void BuildProject()
{
var editorText = @"Module Program
Expand All @@ -30,7 +28,7 @@ End Sub
End Module";

_visualStudio.Instance.Editor.SetText(editorText);
VisualStudio.Instance.Editor.SetText(editorText);

// TODO: Validate build works as expected
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ private static IDictionary<string, string> InitializeVisualBasicProjectTemplates
};
}

public string DirectoryName => Path.GetDirectoryName(FileName);
public string DirectoryName => Path.GetDirectoryName(SolutionFileFullPath);

public string FileName
public string SolutionFileFullPath
{
get
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public SolutionExplorer_OutOfProc(VisualStudioInstance visualStudioInstance)
public void CloseSolution(bool saveFirst = false)
=> _inProc.CloseSolution(saveFirst);

/// <summary>
/// The full file path to the solution file.
/// </summary>
public string SolutionFileFullPath => _inProc.SolutionFileFullPath;

/// <summary>
/// Creates and loads a new solution in the host process, optionally saving the existing solution if one exists.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Ipc;
Expand Down Expand Up @@ -43,10 +45,23 @@ public class VisualStudioInstance

internal Process HostProcess { get; }

public VisualStudioInstance(Process hostProcess, DTE dte)
/// <summary>
/// The set of Visual Studio packages that are installed into this instance.
/// </summary>
public ImmutableHashSet<string> SupportedPackageIds { get; }

/// <summary>
/// The path to the root of this installed version of Visual Studio. This is the folder that contains
/// Common7\IDE.
/// </summary>
public string InstallationPath { get; }

public VisualStudioInstance(Process hostProcess, DTE dte, ImmutableHashSet<string> supportedPackageIds, string installationPath)
{
HostProcess = hostProcess;
Dte = dte;
SupportedPackageIds = supportedPackageIds;
InstallationPath = installationPath;

StartRemoteIntegrationService(dte);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ public sealed class VisualStudioInstanceFactory : IDisposable
/// The instance that has already been launched by this factory and can be reused.
/// </summary>
private VisualStudioInstance _currentlyRunningInstance;
private ImmutableHashSet<string> _supportedPackageIds;
private string _installationPath;

private bool _hasCurrentlyActiveContext;

static VisualStudioInstanceFactory()
Expand Down Expand Up @@ -123,7 +122,7 @@ private bool ShouldStartNewInstance(ImmutableHashSet<string> requiredPackageIds)
// * The current instance is no longer running

return _currentlyRunningInstance == null
|| (_supportedPackageIds != null && !requiredPackageIds.All((requiredPackageId) => _supportedPackageIds.Contains(requiredPackageId))) // _supportedPackagesIds will be null if ISetupInstance2.GetPackages() is NYI
|| (!requiredPackageIds.All(id => _currentlyRunningInstance.SupportedPackageIds.Contains(id)))
|| !_currentlyRunningInstance.IsRunning;
}

Expand All @@ -142,17 +141,19 @@ private void UpdateCurrentlyRunningInstance(ImmutableHashSet<string> requiredPac
{
Process hostProcess;
DTE dte;
ImmutableHashSet<string> supportedPackageIds;
string installationPath;

if (shouldStartNewInstance)
{
// We are starting a new instance, so ensure we close the currently running instance, if it exists
_currentlyRunningInstance?.Close();

var instance = LocateVisualStudioInstance(requiredPackageIds) as ISetupInstance2;
_supportedPackageIds = ImmutableHashSet.CreateRange(instance.GetPackages().Select((supportedPackage) => supportedPackage.GetId()));
_installationPath = instance.GetInstallationPath();
supportedPackageIds = ImmutableHashSet.CreateRange(instance.GetPackages().Select((supportedPackage) => supportedPackage.GetId()));
installationPath = instance.GetInstallationPath();

hostProcess = StartNewVisualStudioProcess(_installationPath);
hostProcess = StartNewVisualStudioProcess(installationPath);
// We wait until the DTE instance is up before we're good
dte = IntegrationHelper.WaitForNotNullAsync(() => IntegrationHelper.TryLocateDteForProcess(hostProcess)).Result;
}
Expand All @@ -166,11 +167,13 @@ private void UpdateCurrentlyRunningInstance(ImmutableHashSet<string> requiredPac

hostProcess = _currentlyRunningInstance.HostProcess;
dte = _currentlyRunningInstance.Dte;
supportedPackageIds = _currentlyRunningInstance.SupportedPackageIds;
installationPath = _currentlyRunningInstance.InstallationPath;

_currentlyRunningInstance.Close(exitHostProcess: false);
}

_currentlyRunningInstance = new VisualStudioInstance(hostProcess, dte);
_currentlyRunningInstance = new VisualStudioInstance(hostProcess, dte, supportedPackageIds, installationPath);
}

private static ISetupConfiguration GetSetupConfiguration()
Expand Down

0 comments on commit 3d21157

Please sign in to comment.