Skip to content

Commit fe3c796

Browse files
Add the output in the exception message when Sqlcmd execution failed.
1 parent 8ad39fd commit fe3c796

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/Testing.Databases.SqlServer.SqlCmd/SqlCmdSqlServerDatabaseExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public static void RunScript(this SqlServerDatabase database, string fileName, S
4747

4848
if (exitCode != 0)
4949
{
50-
throw new SqlCmdException($"Some errors has been occurred when executing the '{fileName}'. Check the {nameof(SqlCmdException.Output)} property of the exception to retrieve the output of the sqlcmd utility.", sqlCmdProcess.Output);
50+
throw new SqlCmdException($"Some errors has been occurred when executing the '{fileName}'.{Environment.NewLine}{Environment.NewLine}-- Output --{Environment.NewLine}{sqlCmdProcess.Output}", sqlCmdProcess.Output);
5151
}
5252
}
5353
}

tests/Testing.Databases.SqlServer.SqlCmd.Tests/SqlCmdSqlServerExtensionsTest.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,19 @@ public void RunScript_WithErros()
119119
CREATE TABLE ErrorBlabla
120120
""");
121121

122-
server.Master.Invoking(m => m.RunScript(temporaryFile.FileName, settings))
123-
.Should().ThrowExactly<SqlCmdException>()
124-
.Which.Output.Should().StartWith(
122+
var exception = server.Master.Invoking(m => m.RunScript(temporaryFile.FileName, settings))
123+
.Should().ThrowExactly<SqlCmdException>();
124+
125+
exception.Which.Output.Should().StartWith(
125126
"""
126127
GOOOOOO !
127128
Changed database context to 'SqlCmdSqlServerExtensionsTest_RunScript_WithErros'.
128129
Msg 102, Level 15, State 1,
129130
""")
130131
.And.EndWith("Incorrect syntax near 'ErrorBlabla'.");
131132

133+
exception.Which.Message.Should().Be($"Some errors has been occurred when executing the '{temporaryFile.FileName}'.{Environment.NewLine}{Environment.NewLine}-- Output --{Environment.NewLine}{exception.Which.Output}");
134+
132135
var database = server.GetDatabase("SqlCmdSqlServerExtensionsTest_RunScript_WithErros");
133136

134137
var table = database.ExecuteQuery("SELECT * FROM sys.tables");

0 commit comments

Comments
 (0)