|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using Cake.Common.Tests.Fixtures.Tools.DotNet.Sln.Add; |
| 7 | +using Cake.Common.Tools.DotNet; |
| 8 | +using Cake.Core.IO; |
| 9 | +using Cake.Testing; |
| 10 | +using Xunit; |
| 11 | + |
| 12 | +namespace Cake.Common.Tests.Unit.Tools.DotNet.Sln.Add |
| 13 | +{ |
| 14 | + public sealed class DotNetSlnAdderTests |
| 15 | + { |
| 16 | + public sealed class TheAddMethod |
| 17 | + { |
| 18 | + [Fact] |
| 19 | + public void Should_Throw_If_Process_Was_Not_Started() |
| 20 | + { |
| 21 | + // Given |
| 22 | + var fixture = new DotNetSlnAdderFixture(); |
| 23 | + fixture.ProjectPath = new[] { (FilePath)"./lib1.csproj" }; |
| 24 | + fixture.GivenProcessCannotStart(); |
| 25 | + |
| 26 | + // When |
| 27 | + var result = Record.Exception(() => fixture.Run()); |
| 28 | + |
| 29 | + // Then |
| 30 | + AssertEx.IsCakeException(result, ".NET CLI: Process was not started."); |
| 31 | + } |
| 32 | + |
| 33 | + [Fact] |
| 34 | + public void Should_Throw_If_Process_Has_A_Non_Zero_Exit_Code() |
| 35 | + { |
| 36 | + // Given |
| 37 | + var fixture = new DotNetSlnAdderFixture(); |
| 38 | + fixture.ProjectPath = new[] { (FilePath)"./lib1.csproj" }; |
| 39 | + fixture.GivenProcessExitsWithCode(1); |
| 40 | + |
| 41 | + // When |
| 42 | + var result = Record.Exception(() => fixture.Run()); |
| 43 | + |
| 44 | + // Then |
| 45 | + AssertEx.IsCakeException(result, ".NET CLI: Process returned an error (exit code 1)."); |
| 46 | + } |
| 47 | + |
| 48 | + [Fact] |
| 49 | + public void Should_Throw_If_ProjectPath_Is_Null() |
| 50 | + { |
| 51 | + // Given |
| 52 | + var fixture = new DotNetSlnAdderFixture(); |
| 53 | + fixture.ProjectPath = null; |
| 54 | + |
| 55 | + // When |
| 56 | + var result = Record.Exception(() => fixture.Run()); |
| 57 | + |
| 58 | + // Then |
| 59 | + AssertEx.IsArgumentNullException(result, "projectPath"); |
| 60 | + } |
| 61 | + |
| 62 | + [Fact] |
| 63 | + public void Should_Throw_If_ProjectPath_Is_Empty() |
| 64 | + { |
| 65 | + // Given |
| 66 | + var fixture = new DotNetSlnAdderFixture(); |
| 67 | + fixture.ProjectPath = []; |
| 68 | + |
| 69 | + // When |
| 70 | + var result = Record.Exception(() => fixture.Run()); |
| 71 | + |
| 72 | + // Then |
| 73 | + AssertEx.IsArgumentNullException(result, "projectPath"); |
| 74 | + } |
| 75 | + |
| 76 | + [Fact] |
| 77 | + public void Should_Throw_If_Settings_Are_Null() |
| 78 | + { |
| 79 | + // Given |
| 80 | + var fixture = new DotNetSlnAdderFixture(); |
| 81 | + fixture.ProjectPath = new[] { (FilePath)"./lib1.csproj" }; |
| 82 | + fixture.Settings = null; |
| 83 | + |
| 84 | + // When |
| 85 | + var result = Record.Exception(() => fixture.Run()); |
| 86 | + |
| 87 | + // Then |
| 88 | + AssertEx.IsArgumentNullException(result, "settings"); |
| 89 | + } |
| 90 | + |
| 91 | + [Fact] |
| 92 | + public void Should_Throw_If_InRoot_And_SolutionFolder_Are_Used_Together() |
| 93 | + { |
| 94 | + // Given |
| 95 | + var fixture = new DotNetSlnAdderFixture(); |
| 96 | + fixture.ProjectPath = new[] { (FilePath)"./lib1.csproj" }; |
| 97 | + fixture.Settings.InRoot = true; |
| 98 | + fixture.Settings.SolutionFolder = "mylibs"; |
| 99 | + |
| 100 | + // When |
| 101 | + var result = Record.Exception(() => fixture.Run()); |
| 102 | + |
| 103 | + // Then |
| 104 | + Assert.IsType<ArgumentException>(result); |
| 105 | + Assert.Equal("InRoot and SolutionFolder cannot be used together.", result.Message); |
| 106 | + } |
| 107 | + |
| 108 | + [Fact] |
| 109 | + public void Should_Add_Solution_Argument() |
| 110 | + { |
| 111 | + // Given |
| 112 | + var fixture = new DotNetSlnAdderFixture(); |
| 113 | + fixture.Solution = (FilePath)"test.sln"; |
| 114 | + fixture.ProjectPath = new[] { (FilePath)"./lib1.csproj" }; |
| 115 | + |
| 116 | + // When |
| 117 | + var result = fixture.Run(); |
| 118 | + |
| 119 | + // Then |
| 120 | + Assert.NotNull(result); |
| 121 | + Assert.Equal("sln \"/Working/test.sln\" add \"/Working/lib1.csproj\"", result.Args); |
| 122 | + } |
| 123 | + |
| 124 | + [Fact] |
| 125 | + public void Should_Not_Add_Solution_Argument() |
| 126 | + { |
| 127 | + // Given |
| 128 | + var fixture = new DotNetSlnAdderFixture(); |
| 129 | + fixture.Solution = null; |
| 130 | + fixture.ProjectPath = new[] { (FilePath)"./lib1.csproj" }; |
| 131 | + |
| 132 | + // When |
| 133 | + var result = fixture.Run(); |
| 134 | + |
| 135 | + // Then |
| 136 | + Assert.NotNull(result); |
| 137 | + Assert.Equal("sln add \"/Working/lib1.csproj\"", result.Args); |
| 138 | + } |
| 139 | + |
| 140 | + [Fact] |
| 141 | + public void Should_Add_ProjectPath_Argument() |
| 142 | + { |
| 143 | + // Given |
| 144 | + var fixture = new DotNetSlnAdderFixture(); |
| 145 | + fixture.ProjectPath = new[] { (FilePath)"./lib1.csproj" }; |
| 146 | + |
| 147 | + // When |
| 148 | + var result = fixture.Run(); |
| 149 | + |
| 150 | + // Then |
| 151 | + Assert.NotNull(result); |
| 152 | + Assert.Equal("sln add \"/Working/lib1.csproj\"", result.Args); |
| 153 | + } |
| 154 | + |
| 155 | + [Fact] |
| 156 | + public void Should_Add_All_ProjectPath() |
| 157 | + { |
| 158 | + // Given |
| 159 | + var fixture = new DotNetSlnAdderFixture(); |
| 160 | + fixture.ProjectPath = new[] { (FilePath)"./lib1.csproj", "./lib2.csproj", "./lib3.csproj" }; |
| 161 | + |
| 162 | + // When |
| 163 | + var result = fixture.Run(); |
| 164 | + |
| 165 | + // Then |
| 166 | + Assert.NotNull(result); |
| 167 | + Assert.Equal("sln add \"/Working/lib1.csproj\" \"/Working/lib2.csproj\" \"/Working/lib3.csproj\"", result.Args); |
| 168 | + } |
| 169 | + |
| 170 | + [Fact] |
| 171 | + public void Should_Add_InRoot_Argument() |
| 172 | + { |
| 173 | + // Given |
| 174 | + var fixture = new DotNetSlnAdderFixture(); |
| 175 | + fixture.ProjectPath = new[] { (FilePath)"./lib1.csproj" }; |
| 176 | + fixture.Settings.InRoot = true; |
| 177 | + |
| 178 | + // When |
| 179 | + var result = fixture.Run(); |
| 180 | + |
| 181 | + // Then |
| 182 | + Assert.NotNull(result); |
| 183 | + Assert.Equal("sln add --in-root \"/Working/lib1.csproj\"", result.Args); |
| 184 | + } |
| 185 | + |
| 186 | + [Fact] |
| 187 | + public void Should_Add_SolutionFolder_Argument() |
| 188 | + { |
| 189 | + // Given |
| 190 | + var fixture = new DotNetSlnAdderFixture(); |
| 191 | + fixture.ProjectPath = new[] { (FilePath)"./lib1.csproj" }; |
| 192 | + fixture.Settings.SolutionFolder = "mylibs"; |
| 193 | + |
| 194 | + // When |
| 195 | + var result = fixture.Run(); |
| 196 | + |
| 197 | + // Then |
| 198 | + Assert.NotNull(result); |
| 199 | + Assert.Equal("sln add --solution-folder \"/Working/mylibs\" \"/Working/lib1.csproj\"", result.Args); |
| 200 | + } |
| 201 | + |
| 202 | + [Fact] |
| 203 | + public void Should_Add_Additional_Arguments() |
| 204 | + { |
| 205 | + // Given |
| 206 | + var fixture = new DotNetSlnAdderFixture(); |
| 207 | + fixture.Solution = (FilePath)"test.sln"; |
| 208 | + fixture.ProjectPath = new[] { (FilePath)"./lib1.csproj" }; |
| 209 | + fixture.Settings.Verbosity = DotNetVerbosity.Detailed; |
| 210 | + |
| 211 | + // When |
| 212 | + var result = fixture.Run(); |
| 213 | + |
| 214 | + // Then |
| 215 | + Assert.NotNull(result); |
| 216 | + Assert.Equal("sln \"/Working/test.sln\" add \"/Working/lib1.csproj\" --verbosity detailed", result.Args); |
| 217 | + } |
| 218 | + } |
| 219 | + } |
| 220 | +} |
0 commit comments