Merge pull request #8142 from peterhuene/sln-add-directory

Support directories for `sln add`, `sln remove`, `add reference`, and `remove reference` commands.
This commit is contained in:
Livar 2017-12-08 11:42:56 -08:00 committed by GitHub
commit 3fe29161af
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
32 changed files with 397 additions and 96 deletions

1
.gitattributes vendored
View file

@ -51,4 +51,5 @@
*.vbproj text=auto
*.fsproj text=auto
*.dbproj text=auto
*.xlf text=auto
*.sln text=auto eol=crlf

View file

@ -0,0 +1,2 @@
This directory is intentionally empty.

View file

@ -0,0 +1,2 @@
This directory is intentionally empty.

View file

@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
</Project>

View file

@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
</Project>

View file

@ -0,0 +1,2 @@
This directory is intentionally empty.

View file

@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
</Project>

View file

@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
</Project>

View file

@ -330,13 +330,14 @@ namespace Microsoft.DotNet.Tools.Common
public static void EnsureAllPathsExist(
IReadOnlyCollection<string> paths,
string pathDoesNotExistLocalizedFormatString)
string pathDoesNotExistLocalizedFormatString,
bool allowDirectories = false)
{
var notExisting = new List<string>();
foreach (var p in paths)
{
if (!File.Exists(p))
if (!File.Exists(p) && (!allowDirectories || !Directory.Exists(p)))
{
notExisting.Add(p);
}

View file

@ -345,9 +345,6 @@
<data name="SolutionDoesNotExist" xml:space="preserve">
<value>Specified solution file {0} does not exist, or there is no solution file in the directory.</value>
</data>
<data name="ReferenceDoesNotExist" xml:space="preserve">
<value>Reference {0} does not exist.</value>
</data>
<data name="ReferenceIsInvalid" xml:space="preserve">
<value>Reference `{0}` is invalid.</value>
</data>
@ -384,6 +381,9 @@
<data name="ProjectAddedToTheSolution" xml:space="preserve">
<value>Project `{0}` added to the solution.</value>
</data>
<data name="ProjectRemovedFromTheSolution" xml:space="preserve">
<value>Project `{0}` removed from the solution.</value>
</data>
<data name="SolutionAlreadyContainsProject" xml:space="preserve">
<value>Solution {0} already contains project {1}.</value>
</data>

View file

@ -221,7 +221,7 @@ namespace Microsoft.DotNet.Tools.Common
if (projectsToRemove.Count == 0)
{
Reporter.Output.WriteLine(string.Format(
CommonLocalizableStrings.ProjectReferenceCouldNotBeFound,
CommonLocalizableStrings.ProjectNotFoundInTheSolution,
projectPath));
}
else
@ -244,7 +244,7 @@ namespace Microsoft.DotNet.Tools.Common
slnFile.Projects.Remove(slnProject);
Reporter.Output.WriteLine(
string.Format(CommonLocalizableStrings.ProjectReferenceRemoved, slnProject.FilePath));
string.Format(CommonLocalizableStrings.ProjectRemovedFromTheSolution, slnProject.FilePath));
}
foreach (var project in slnFile.Projects)

View file

@ -45,9 +45,9 @@ namespace Microsoft.DotNet.Tools.Add.ProjectToProjectReference
var frameworkString = _appliedCommand.ValueOrDefault<string>("framework");
PathUtility.EnsureAllPathsExist(_appliedCommand.Arguments, CommonLocalizableStrings.ReferenceDoesNotExist);
PathUtility.EnsureAllPathsExist(_appliedCommand.Arguments, CommonLocalizableStrings.CouldNotFindProjectOrDirectory, true);
List<MsbuildProject> refs = _appliedCommand.Arguments
.Select((r) => MsbuildProject.FromFile(projects, r))
.Select((r) => MsbuildProject.FromFileOrDirectory(projects, r))
.ToList();
if (frameworkString == null)
@ -90,9 +90,10 @@ namespace Microsoft.DotNet.Tools.Add.ProjectToProjectReference
}
}
var relativePathReferences = _appliedCommand.Arguments.Select((r) =>
Path.GetRelativePath(msbuildProj.ProjectDirectory, Path.GetFullPath(r)))
.ToList();
var relativePathReferences = refs.Select((r) =>
Path.GetRelativePath(
msbuildProj.ProjectDirectory,
r.ProjectRootElement.FullPath)).ToList();
int numberOfAddedReferences = msbuildProj.AddProjectToProjectReferences(
frameworkString,

View file

@ -2,6 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.IO;
using System.Linq;
using Microsoft.Build.Evaluation;
using Microsoft.DotNet.Cli;
@ -42,10 +43,22 @@ namespace Microsoft.DotNet.Tools.Remove.ProjectToProjectReference
public override int Execute()
{
var msbuildProj = MsbuildProject.FromFileOrDirectory(new ProjectCollection(), _fileOrDirectory);
var references = _appliedCommand.Arguments.Select(p => {
var fullPath = Path.GetFullPath(p);
if (!Directory.Exists(fullPath))
{
return p;
}
return Path.GetRelativePath(
msbuildProj.ProjectRootElement.FullPath,
MsbuildProject.GetProjectFileFromDirectory(fullPath).FullName
);
});
int numberOfRemovedReferences = msbuildProj.RemoveProjectToProjectReferences(
_appliedCommand.ValueOrDefault<string>("framework"),
_appliedCommand.Arguments);
references);
if (numberOfRemovedReferences != 0)
{

View file

@ -40,11 +40,14 @@ namespace Microsoft.DotNet.Tools.Sln.Add
throw new GracefulException(CommonLocalizableStrings.SpecifyAtLeastOneProjectToAdd);
}
PathUtility.EnsureAllPathsExist(_appliedCommand.Arguments, CommonLocalizableStrings.ProjectDoesNotExist);
PathUtility.EnsureAllPathsExist(_appliedCommand.Arguments, CommonLocalizableStrings.CouldNotFindProjectOrDirectory, true);
var fullProjectPaths = _appliedCommand.Arguments
.Select(Path.GetFullPath)
.ToList();
var fullProjectPaths = _appliedCommand.Arguments.Select(p => {
var fullPath = Path.GetFullPath(p);
return Directory.Exists(fullPath) ?
MsbuildProject.GetProjectFileFromDirectory(fullPath).FullName :
fullPath;
}).ToList();
var preAddProjectCount = slnFile.Projects.Count;

View file

@ -40,11 +40,16 @@ namespace Microsoft.DotNet.Tools.Sln.Remove
{
SlnFile slnFile = SlnFileFactory.CreateFromFileOrDirectory(_fileOrDirectory);
var relativeProjectPaths = _appliedCommand.Arguments.Select(p =>
Path.GetRelativePath(
PathUtility.EnsureTrailingSlash(slnFile.BaseDirectory),
Path.GetFullPath(p)))
.ToList();
var baseDirectory = PathUtility.EnsureTrailingSlash(slnFile.BaseDirectory);
var relativeProjectPaths = _appliedCommand.Arguments.Select(p => {
var fullPath = Path.GetFullPath(p);
return Path.GetRelativePath(
baseDirectory,
Directory.Exists(fullPath) ?
MsbuildProject.GetProjectFileFromDirectory(fullPath).FullName :
fullPath
);
});
bool slnChanged = false;
foreach (var path in relativeProjectPaths)

View file

@ -97,11 +97,6 @@
<target state="translated">Aplikace</target>
<note />
</trans-unit>
<trans-unit id="ReferenceDoesNotExist">
<source>Reference {0} does not exist.</source>
<target state="translated">Odkaz na {0} neexistuje.</target>
<note />
</trans-unit>
<trans-unit id="ReferenceAddedToTheProject">
<source>Reference `{0}` added to the project.</source>
<target state="translated">Odkaz na {0} byl přidán do projektu.</target>
@ -674,6 +669,11 @@
<target state="translated">Při spuštění příkazu neprovede implicitní obnovení.</target>
<note />
</trans-unit>
<trans-unit id="ProjectRemovedFromTheSolution">
<source>Project `{0}` removed from the solution.</source>
<target state="new">Project `{0}` removed from the solution.</target>
<note />
</trans-unit>
</body>
</file>
</xliff>

View file

@ -97,11 +97,6 @@
<target state="translated">Anwendung</target>
<note />
</trans-unit>
<trans-unit id="ReferenceDoesNotExist">
<source>Reference {0} does not exist.</source>
<target state="translated">Der Verweis "{0}" ist nicht vorhanden.</target>
<note />
</trans-unit>
<trans-unit id="ReferenceAddedToTheProject">
<source>Reference `{0}` added to the project.</source>
<target state="translated">Der Verweis "{0}" wurde dem Projekt hinzugefügt.</target>
@ -674,6 +669,11 @@
<target state="translated">Führt beim Ausführen des Befehls keine implizite Wiederherstellung durch.</target>
<note />
</trans-unit>
<trans-unit id="ProjectRemovedFromTheSolution">
<source>Project `{0}` removed from the solution.</source>
<target state="new">Project `{0}` removed from the solution.</target>
<note />
</trans-unit>
</body>
</file>
</xliff>

View file

@ -97,11 +97,6 @@
<target state="translated">Aplicación</target>
<note />
</trans-unit>
<trans-unit id="ReferenceDoesNotExist">
<source>Reference {0} does not exist.</source>
<target state="translated">La referencia {0} no existe.</target>
<note />
</trans-unit>
<trans-unit id="ReferenceAddedToTheProject">
<source>Reference `{0}` added to the project.</source>
<target state="translated">Se ha agregado la referencia "{0}" al proyecto.</target>
@ -674,6 +669,11 @@
<target state="translated">No realiza una restauración implícita al ejecutar el comando.</target>
<note />
</trans-unit>
<trans-unit id="ProjectRemovedFromTheSolution">
<source>Project `{0}` removed from the solution.</source>
<target state="new">Project `{0}` removed from the solution.</target>
<note />
</trans-unit>
</body>
</file>
</xliff>

View file

@ -97,11 +97,6 @@
<target state="translated">Application</target>
<note />
</trans-unit>
<trans-unit id="ReferenceDoesNotExist">
<source>Reference {0} does not exist.</source>
<target state="translated">La référence {0} n'existe pas.</target>
<note />
</trans-unit>
<trans-unit id="ReferenceAddedToTheProject">
<source>Reference `{0}` added to the project.</source>
<target state="translated">Référence '{0}' ajoutée au projet.</target>
@ -674,6 +669,11 @@
<target state="translated">Ne fait pas de restauration implicite durant l'exécution de la commande.</target>
<note />
</trans-unit>
<trans-unit id="ProjectRemovedFromTheSolution">
<source>Project `{0}` removed from the solution.</source>
<target state="new">Project `{0}` removed from the solution.</target>
<note />
</trans-unit>
</body>
</file>
</xliff>

View file

@ -97,11 +97,6 @@
<target state="translated">Applicazione</target>
<note />
</trans-unit>
<trans-unit id="ReferenceDoesNotExist">
<source>Reference {0} does not exist.</source>
<target state="translated">Il riferimento {0} non esiste.</target>
<note />
</trans-unit>
<trans-unit id="ReferenceAddedToTheProject">
<source>Reference `{0}` added to the project.</source>
<target state="translated">Il riferimento `{0}` è stato aggiunto al progetto.</target>
@ -674,6 +669,11 @@
<target state="translated">Non esegue un ripristino implicito durante l'esecuzione del comando.</target>
<note />
</trans-unit>
<trans-unit id="ProjectRemovedFromTheSolution">
<source>Project `{0}` removed from the solution.</source>
<target state="new">Project `{0}` removed from the solution.</target>
<note />
</trans-unit>
</body>
</file>
</xliff>

View file

@ -97,11 +97,6 @@
<target state="translated">アプリケーション</target>
<note />
</trans-unit>
<trans-unit id="ReferenceDoesNotExist">
<source>Reference {0} does not exist.</source>
<target state="translated">参照 {0} は存在しません。</target>
<note />
</trans-unit>
<trans-unit id="ReferenceAddedToTheProject">
<source>Reference `{0}` added to the project.</source>
<target state="translated">参照 `{0}` がプロジェクトに追加されました。</target>
@ -674,6 +669,11 @@
<target state="translated">コマンドを実行するときに暗黙的復元を行いません。</target>
<note />
</trans-unit>
<trans-unit id="ProjectRemovedFromTheSolution">
<source>Project `{0}` removed from the solution.</source>
<target state="new">Project `{0}` removed from the solution.</target>
<note />
</trans-unit>
</body>
</file>
</xliff>

View file

@ -97,11 +97,6 @@
<target state="translated">응용 프로그램</target>
<note />
</trans-unit>
<trans-unit id="ReferenceDoesNotExist">
<source>Reference {0} does not exist.</source>
<target state="translated">{0} 참조가 없습니다.</target>
<note />
</trans-unit>
<trans-unit id="ReferenceAddedToTheProject">
<source>Reference `{0}` added to the project.</source>
<target state="translated">프로젝트에 '{0}' 참조가 추가되었습니다.</target>
@ -674,6 +669,11 @@
<target state="translated">명령을 실행할 때 암시적 복원을 수행하지 않습니다.</target>
<note />
</trans-unit>
<trans-unit id="ProjectRemovedFromTheSolution">
<source>Project `{0}` removed from the solution.</source>
<target state="new">Project `{0}` removed from the solution.</target>
<note />
</trans-unit>
</body>
</file>
</xliff>

View file

@ -97,11 +97,6 @@
<target state="translated">Aplikacja</target>
<note />
</trans-unit>
<trans-unit id="ReferenceDoesNotExist">
<source>Reference {0} does not exist.</source>
<target state="translated">Odwołanie {0} nie istnieje.</target>
<note />
</trans-unit>
<trans-unit id="ReferenceAddedToTheProject">
<source>Reference `{0}` added to the project.</source>
<target state="translated">Do projektu zostało dodane odwołanie „{0}”.</target>
@ -674,6 +669,11 @@
<target state="translated">Nie wykonuje niejawnego przywracania podczas wykonywania polecenia.</target>
<note />
</trans-unit>
<trans-unit id="ProjectRemovedFromTheSolution">
<source>Project `{0}` removed from the solution.</source>
<target state="new">Project `{0}` removed from the solution.</target>
<note />
</trans-unit>
</body>
</file>
</xliff>

View file

@ -97,11 +97,6 @@
<target state="translated">Aplicativo</target>
<note />
</trans-unit>
<trans-unit id="ReferenceDoesNotExist">
<source>Reference {0} does not exist.</source>
<target state="translated">A referência {0} não existe.</target>
<note />
</trans-unit>
<trans-unit id="ReferenceAddedToTheProject">
<source>Reference `{0}` added to the project.</source>
<target state="translated">A referência {0} foi adicionada ao projeto.</target>
@ -674,6 +669,11 @@
<target state="translated">Não faz uma restauração implícita ao executar o comando.</target>
<note />
</trans-unit>
<trans-unit id="ProjectRemovedFromTheSolution">
<source>Project `{0}` removed from the solution.</source>
<target state="new">Project `{0}` removed from the solution.</target>
<note />
</trans-unit>
</body>
</file>
</xliff>

View file

@ -97,11 +97,6 @@
<target state="translated">Приложение</target>
<note />
</trans-unit>
<trans-unit id="ReferenceDoesNotExist">
<source>Reference {0} does not exist.</source>
<target state="translated">Ссылка {0} не существует.</target>
<note />
</trans-unit>
<trans-unit id="ReferenceAddedToTheProject">
<source>Reference `{0}` added to the project.</source>
<target state="translated">Ссылка "{0}" добавлена в проект.</target>
@ -674,6 +669,11 @@
<target state="translated">Не выполняет неявное восстановление при выполнении команды.</target>
<note />
</trans-unit>
<trans-unit id="ProjectRemovedFromTheSolution">
<source>Project `{0}` removed from the solution.</source>
<target state="new">Project `{0}` removed from the solution.</target>
<note />
</trans-unit>
</body>
</file>
</xliff>

View file

@ -97,11 +97,6 @@
<target state="translated">Uygulama</target>
<note />
</trans-unit>
<trans-unit id="ReferenceDoesNotExist">
<source>Reference {0} does not exist.</source>
<target state="translated">{0} başvurusu yok.</target>
<note />
</trans-unit>
<trans-unit id="ReferenceAddedToTheProject">
<source>Reference `{0}` added to the project.</source>
<target state="translated">`{0}` başvurusu projeye eklendi.</target>
@ -674,6 +669,11 @@
<target state="translated">Komut yürütülürken örtük geri yükleme gerçekleştirmez.</target>
<note />
</trans-unit>
<trans-unit id="ProjectRemovedFromTheSolution">
<source>Project `{0}` removed from the solution.</source>
<target state="new">Project `{0}` removed from the solution.</target>
<note />
</trans-unit>
</body>
</file>
</xliff>

View file

@ -97,11 +97,6 @@
<target state="translated">应用程序</target>
<note />
</trans-unit>
<trans-unit id="ReferenceDoesNotExist">
<source>Reference {0} does not exist.</source>
<target state="translated">引用 {0} 不存在。</target>
<note />
</trans-unit>
<trans-unit id="ReferenceAddedToTheProject">
<source>Reference `{0}` added to the project.</source>
<target state="translated">已将引用“{0}”添加到项目。</target>
@ -674,6 +669,11 @@
<target state="translated">请勿在执行命令时进行隐式还原。</target>
<note />
</trans-unit>
<trans-unit id="ProjectRemovedFromTheSolution">
<source>Project `{0}` removed from the solution.</source>
<target state="new">Project `{0}` removed from the solution.</target>
<note />
</trans-unit>
</body>
</file>
</xliff>

View file

@ -97,11 +97,6 @@
<target state="translated">應用程式</target>
<note />
</trans-unit>
<trans-unit id="ReferenceDoesNotExist">
<source>Reference {0} does not exist.</source>
<target state="translated">參考 {0} 不存在。</target>
<note />
</trans-unit>
<trans-unit id="ReferenceAddedToTheProject">
<source>Reference `{0}` added to the project.</source>
<target state="translated">參考 `{0}` 已新增至專案。</target>
@ -674,6 +669,11 @@
<target state="translated">執行此命令時,請勿進行隱含還原。</target>
<note />
</trans-unit>
<trans-unit id="ProjectRemovedFromTheSolution">
<source>Project `{0}` removed from the solution.</source>
<target state="new">Project `{0}` removed from the solution.</target>
<note />
</trans-unit>
</body>
</file>
</xliff>

View file

@ -559,7 +559,7 @@ Commands:
.WithProject(lib.CsProjName)
.Execute("\"IDoNotExist.csproj\"");
cmd.Should().Fail();
cmd.StdErr.Should().Be(string.Format(CommonLocalizableStrings.ReferenceDoesNotExist, "IDoNotExist.csproj"));
cmd.StdErr.Should().Be(string.Format(CommonLocalizableStrings.CouldNotFindProjectOrDirectory, "IDoNotExist.csproj"));
lib.CsProjContent().Should().BeEquivalentTo(contentBefore);
}
@ -575,7 +575,7 @@ Commands:
.WithProject(lib.CsProjPath)
.Execute($"\"{setup.ValidRefCsprojPath}\" \"IDoNotExist.csproj\"");
cmd.Should().Fail();
cmd.StdErr.Should().Be(string.Format(CommonLocalizableStrings.ReferenceDoesNotExist, "IDoNotExist.csproj"));
cmd.StdErr.Should().Be(string.Format(CommonLocalizableStrings.CouldNotFindProjectOrDirectory, "IDoNotExist.csproj"));
lib.CsProjContent().Should().BeEquivalentTo(contentBefore);
}
@ -693,5 +693,55 @@ Commands:
cmd.StdErr.Should().MatchRegex(" - net45");
net45lib.CsProjContent().Should().BeEquivalentTo(csProjContent);
}
[Fact]
public void WhenDirectoryContainingProjectIsGivenReferenceIsAdded()
{
var setup = Setup();
var lib = NewLibWithFrameworks(dir: setup.TestRoot);
var result = new AddReferenceCommand()
.WithWorkingDirectory(setup.TestRoot)
.WithProject(lib.CsProjPath)
.Execute($"\"{Path.GetDirectoryName(setup.ValidRefCsprojPath)}\"");
result.Should().Pass();
result.StdOut.Should().Be(string.Format(CommonLocalizableStrings.ReferenceAddedToTheProject, @"ValidRef\ValidRef.csproj"));
result.StdErr.Should().BeEmpty();
}
[Fact]
public void WhenDirectoryContainsNoProjectsItCancelsWholeOperation()
{
var setup = Setup();
var lib = NewLibWithFrameworks(dir: setup.TestRoot);
var reference = "Empty";
var result = new AddReferenceCommand()
.WithWorkingDirectory(setup.TestRoot)
.WithProject(lib.CsProjPath)
.Execute(reference);
result.Should().Fail();
result.StdOut.Should().BeVisuallyEquivalentToIfNotLocalized(HelpText);
result.StdErr.Should().Be(string.Format(CommonLocalizableStrings.CouldNotFindAnyProjectInDirectory, reference));
}
[Fact]
public void WhenDirectoryContainsMultipleProjectsItCancelsWholeOperation()
{
var setup = Setup();
var lib = NewLibWithFrameworks(dir: setup.TestRoot);
var reference = "MoreThanOne";
var result = new AddReferenceCommand()
.WithWorkingDirectory(setup.TestRoot)
.WithProject(lib.CsProjPath)
.Execute(reference);
result.Should().Fail();
result.StdOut.Should().BeVisuallyEquivalentToIfNotLocalized(HelpText);
result.StdErr.Should().Be(string.Format(CommonLocalizableStrings.MoreThanOneProjectInDirectory, reference));
}
}
}

View file

@ -506,5 +506,56 @@ Commands:
csproj.NumberOfItemGroupsWithoutCondition().Should().Be(noCondBefore - 1);
csproj.NumberOfProjectReferencesWithIncludeContaining(validref.Name).Should().Be(0);
}
[Fact]
public void WhenDirectoryContainingProjectIsGivenReferenceIsRemoved()
{
var setup = Setup();
var lib = NewLibWithFrameworks(dir: setup.TestRoot);
var libref = AddLibRef(setup, lib);
var result = new RemoveReferenceCommand()
.WithWorkingDirectory(setup.TestRoot)
.WithProject(lib.CsProjPath)
.Execute($"\"{libref.CsProjPath}\"");
result.Should().Pass();
result.StdOut.Should().Be(string.Format(CommonLocalizableStrings.ProjectReferenceRemoved, Path.Combine("Lib", setup.LibCsprojName)));
result.StdErr.Should().BeEmpty();
}
[Fact]
public void WhenDirectoryContainsNoProjectsItCancelsWholeOperation()
{
var setup = Setup();
var lib = NewLibWithFrameworks(dir: setup.TestRoot);
var reference = "Empty";
var result = new RemoveReferenceCommand()
.WithWorkingDirectory(setup.TestRoot)
.WithProject(lib.CsProjPath)
.Execute(reference);
result.Should().Fail();
result.StdOut.Should().BeVisuallyEquivalentToIfNotLocalized(HelpText);
result.StdErr.Should().Be(string.Format(CommonLocalizableStrings.CouldNotFindAnyProjectInDirectory, Path.Combine(setup.TestRoot, reference)));
}
[Fact]
public void WhenDirectoryContainsMultipleProjectsItCancelsWholeOperation()
{
var setup = Setup();
var lib = NewLibWithFrameworks(dir: setup.TestRoot);
var reference = "MoreThanOne";
var result = new RemoveReferenceCommand()
.WithWorkingDirectory(setup.TestRoot)
.WithProject(lib.CsProjPath)
.Execute(reference);
result.Should().Fail();
result.StdOut.Should().BeVisuallyEquivalentToIfNotLocalized(HelpText);
result.StdErr.Should().Be(string.Format(CommonLocalizableStrings.MoreThanOneProjectInDirectory, Path.Combine(setup.TestRoot, reference)));
}
}
}

View file

@ -359,6 +359,81 @@ EndGlobal
.Should().BeVisuallyEquivalentTo(expectedSlnContents);
}
[Fact]
public void WhenDirectoryContainingProjectIsGivenProjectIsAdded()
{
var projectDirectory = TestAssets
.Get("TestAppWithSlnAndCsprojFiles")
.CreateInstance()
.WithSourceFiles()
.Root
.FullName;
var cmd = new DotnetCommand()
.WithWorkingDirectory(projectDirectory)
.ExecuteWithCapturedOutput("sln add Lib");
cmd.Should().Pass();
var slnPath = Path.Combine(projectDirectory, "App.sln");
var expectedSlnContents = GetExpectedSlnContents(slnPath, ExpectedSlnFileAfterAddingLibProj);
File.ReadAllText(slnPath)
.Should().BeVisuallyEquivalentTo(expectedSlnContents);
}
[Fact]
public void WhenDirectoryContainsNoProjectsItCancelsWholeOperation()
{
var projectDirectory = TestAssets
.Get("TestAppWithSlnAndCsprojFiles")
.CreateInstance()
.WithSourceFiles()
.Root
.FullName;
var slnFullPath = Path.Combine(projectDirectory, "App.sln");
var contentBefore = File.ReadAllText(slnFullPath);
var directoryToAdd = "Empty";
var cmd = new DotnetCommand()
.WithWorkingDirectory(projectDirectory)
.ExecuteWithCapturedOutput($"sln add {directoryToAdd}");
cmd.Should().Fail();
cmd.StdErr.Should().Be(
string.Format(
CommonLocalizableStrings.CouldNotFindAnyProjectInDirectory,
Path.Combine(projectDirectory, directoryToAdd)));
File.ReadAllText(slnFullPath)
.Should().BeVisuallyEquivalentTo(contentBefore);
}
[Fact]
public void WhenDirectoryContainsMultipleProjectsItCancelsWholeOperation()
{
var projectDirectory = TestAssets
.Get("TestAppWithSlnAndCsprojFiles")
.CreateInstance()
.WithSourceFiles()
.Root
.FullName;
var slnFullPath = Path.Combine(projectDirectory, "App.sln");
var contentBefore = File.ReadAllText(slnFullPath);
var directoryToAdd = "Multiple";
var cmd = new DotnetCommand()
.WithWorkingDirectory(projectDirectory)
.ExecuteWithCapturedOutput($"sln add {directoryToAdd}");
cmd.Should().Fail();
cmd.StdErr.Should().Be(
string.Format(
CommonLocalizableStrings.MoreThanOneProjectInDirectory,
Path.Combine(projectDirectory, directoryToAdd)));
File.ReadAllText(slnFullPath)
.Should().BeVisuallyEquivalentTo(contentBefore);
}
[Fact]
public void WhenProjectDirectoryIsAddedSolutionFoldersAreNotCreated()
{
@ -597,7 +672,7 @@ EndGlobal
.WithWorkingDirectory(projectDirectory)
.ExecuteWithCapturedOutput($"sln App.sln add {projectToAdd} idonotexist.csproj");
cmd.Should().Fail();
cmd.StdErr.Should().Be(string.Format(CommonLocalizableStrings.ProjectDoesNotExist, "idonotexist.csproj"));
cmd.StdErr.Should().Be(string.Format(CommonLocalizableStrings.CouldNotFindProjectOrDirectory, "idonotexist.csproj"));
File.ReadAllText(slnFullPath)
.Should().BeVisuallyEquivalentTo(contentBefore);

View file

@ -367,7 +367,7 @@ EndGlobal
.WithWorkingDirectory(projectDirectory)
.ExecuteWithCapturedOutput("sln remove referenceDoesNotExistInSln.csproj");
cmd.Should().Pass();
cmd.StdOut.Should().Be(string.Format(CommonLocalizableStrings.ProjectReferenceCouldNotBeFound, "referenceDoesNotExistInSln.csproj"));
cmd.StdOut.Should().Be(string.Format(CommonLocalizableStrings.ProjectNotFoundInTheSolution, "referenceDoesNotExistInSln.csproj"));
File.ReadAllText(solutionPath)
.Should().BeVisuallyEquivalentTo(contentBefore);
}
@ -391,7 +391,7 @@ EndGlobal
.WithWorkingDirectory(projectDirectory)
.ExecuteWithCapturedOutput($"sln remove {projectToRemove}");
cmd.Should().Pass();
cmd.StdOut.Should().Be(string.Format(CommonLocalizableStrings.ProjectReferenceRemoved, projectToRemove));
cmd.StdOut.Should().Be(string.Format(CommonLocalizableStrings.ProjectRemovedFromTheSolution, projectToRemove));
slnFile = SlnFile.Read(solutionPath);
slnFile.Projects.Count.Should().Be(1);
@ -418,7 +418,7 @@ EndGlobal
.ExecuteWithCapturedOutput($"sln remove {projectToRemove}");
cmd.Should().Pass();
string outputText = string.Format(CommonLocalizableStrings.ProjectReferenceRemoved, projectToRemove);
string outputText = string.Format(CommonLocalizableStrings.ProjectRemovedFromTheSolution, projectToRemove);
outputText += Environment.NewLine + outputText;
cmd.StdOut.Should().BeVisuallyEquivalentTo(outputText);
@ -447,9 +447,9 @@ EndGlobal
.ExecuteWithCapturedOutput($"sln remove idontexist.csproj {projectToRemove} idontexisteither.csproj");
cmd.Should().Pass();
string outputText = $@"{string.Format(CommonLocalizableStrings.ProjectReferenceCouldNotBeFound, "idontexist.csproj")}
{string.Format(CommonLocalizableStrings.ProjectReferenceRemoved, projectToRemove)}
{string.Format(CommonLocalizableStrings.ProjectReferenceCouldNotBeFound, "idontexisteither.csproj")}";
string outputText = $@"{string.Format(CommonLocalizableStrings.ProjectNotFoundInTheSolution, "idontexist.csproj")}
{string.Format(CommonLocalizableStrings.ProjectRemovedFromTheSolution, projectToRemove)}
{string.Format(CommonLocalizableStrings.ProjectNotFoundInTheSolution, "idontexisteither.csproj")}";
cmd.StdOut.Should().BeVisuallyEquivalentTo(outputText);
@ -482,6 +482,73 @@ EndGlobal
.Should().BeVisuallyEquivalentTo(ExpectedSlnContentsAfterRemove);
}
[Fact]
public void WhenDirectoryContainingProjectIsGivenProjectIsRemoved()
{
var projectDirectory = TestAssets
.Get("TestAppWithSlnAndCsprojToRemove")
.CreateInstance()
.WithSourceFiles()
.Root
.FullName;
var solutionPath = Path.Combine(projectDirectory, "App.sln");
SlnFile slnFile = SlnFile.Read(solutionPath);
slnFile.Projects.Count.Should().Be(2);
var cmd = new DotnetCommand()
.WithWorkingDirectory(projectDirectory)
.ExecuteWithCapturedOutput("sln remove Lib");
cmd.Should().Pass();
File.ReadAllText(solutionPath)
.Should().BeVisuallyEquivalentTo(ExpectedSlnContentsAfterRemove);
}
[Fact]
public void WhenDirectoryContainsNoProjectsItCancelsWholeOperation()
{
var projectDirectory = TestAssets
.Get("TestAppWithSlnAndCsprojToRemove")
.CreateInstance()
.WithSourceFiles()
.Root
.FullName;
var directoryToRemove = "Empty";
var cmd = new DotnetCommand()
.WithWorkingDirectory(projectDirectory)
.ExecuteWithCapturedOutput($"sln remove {directoryToRemove}");
cmd.Should().Fail();
cmd.StdErr.Should().Be(
string.Format(
CommonLocalizableStrings.CouldNotFindAnyProjectInDirectory,
Path.Combine(projectDirectory, directoryToRemove)));
cmd.StdOut.Should().BeVisuallyEquivalentToIfNotLocalized(HelpText);
}
[Fact]
public void WhenDirectoryContainsMultipleProjectsItCancelsWholeOperation()
{
var projectDirectory = TestAssets
.Get("TestAppWithSlnAndCsprojToRemove")
.CreateInstance()
.WithSourceFiles()
.Root
.FullName;
var directoryToRemove = "Multiple";
var cmd = new DotnetCommand()
.WithWorkingDirectory(projectDirectory)
.ExecuteWithCapturedOutput($"sln remove {directoryToRemove}");
cmd.Should().Fail();
cmd.StdErr.Should().Be(
string.Format(
CommonLocalizableStrings.MoreThanOneProjectInDirectory,
Path.Combine(projectDirectory, directoryToRemove)));
cmd.StdOut.Should().BeVisuallyEquivalentToIfNotLocalized(HelpText);
}
[Fact]
public void WhenReferenceIsRemovedSlnBuilds()
{