Skip to content

Commit 8ad39fd

Browse files
Add guard for public methods in SqlCmd project.
1 parent 03bee0e commit 8ad39fd

7 files changed

Lines changed: 128 additions & 2 deletions

File tree

src/Directory.Build.props

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111
<PackageReadmeFile>README.md</PackageReadmeFile>
1212
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1313
<PackageReleaseNotes>
14+
3.0.0
15+
- Add new PosInformatique.Testing.Databases.SqlCmd to initialize database using sqlcmd utility.
16+
- For DACPAC deployment or database creation it is now possible to specify the location of the data and log files.
17+
18+
2.3.0
19+
- Add the support to compare the seed and increment for the IDENTITY columns
20+
1421
2.2.0
1522
- Add SqlServerDatabase.ClearDataAsync() method.
1623
- Add SqlServer.CreateDatabase() method to create database from an Entity Framework DbContext.

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,22 @@ public static class SqlCmdDatabaseInitializer
2626
/// <param name="fileName">Full path of the T-SQL file to execute.</param>
2727
/// <param name="connectionString">Connection string to the SQL Server with administrator rights.</param>
2828
/// <param name="settings">Additionnal settings to run the <c>sqlcmd</c> tool.</param>
29+
/// <exception cref="ArgumentNullException">If the specified <paramref name="initializer"/> argument is <see langword="null"/>.</exception>
30+
/// <exception cref="ArgumentNullException">If the specified <paramref name="fileName"/> argument is <see langword="null"/>.</exception>
31+
/// <exception cref="ArgumentNullException">If the specified <paramref name="connectionString"/> argument is <see langword="null"/>.</exception>
32+
/// <exception cref="FileNotFoundException">If no file exists with the specified <paramref name="fileName"/> argument.</exception>
2933
/// <returns>An instance of the <see cref="SqlServerDatabase"/> which allows to perform query to initialize the data.</returns>
3034
public static SqlServerDatabase Initialize(this SqlServerDatabaseInitializer initializer, string fileName, string connectionString, SqlCmdRunScriptSettings? settings = null)
3135
{
36+
ArgumentNullException.ThrowIfNull(initializer, nameof(initializer));
37+
ArgumentNullException.ThrowIfNull(fileName, nameof(fileName));
38+
ArgumentNullException.ThrowIfNull(connectionString, nameof(connectionString));
39+
40+
if (!File.Exists(fileName))
41+
{
42+
throw new FileNotFoundException($"Could not find file '{fileName}'", fileName);
43+
}
44+
3245
var connectionStringBuilder = new SqlConnectionStringBuilder(connectionString);
3346

3447
var server = new SqlServer(connectionString);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ namespace PosInformatique.Testing.Databases.SqlServer
1111

1212
internal sealed class SqlCmdProcess : IDisposable
1313
{
14-
private Process? process;
14+
private readonly List<string> output;
1515

16-
private List<string> output;
16+
private Process? process;
1717

1818
private SqlCmdProcess(string arguments)
1919
{

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,21 @@ public static class SqlCmdSqlServerDatabaseExtensions
1919
/// <param name="database"><paramref name="database"/> where the script will be executed on.</param>
2020
/// <param name="fileName">T-SQL script to execute on the <paramref name="database"/>.</param>
2121
/// <param name="settings">Additional settings to run the script.</param>
22+
/// <exception cref="ArgumentNullException">If the specified <paramref name="database"/> argument is <see langword="null"/>.</exception>
23+
/// <exception cref="ArgumentNullException">If the specified <paramref name="fileName"/> argument is <see langword="null"/>.</exception>
24+
/// <exception cref="FileNotFoundException">If no file exists with the specified <paramref name="fileName"/> argument.</exception>
2225
/// <exception cref="SqlCmdException">If an error has been occured when running the T-SQL script. Check the <see cref="SqlCmdException.Output"/>
2326
/// to retrieve the output result of the script execution.</exception>
2427
public static void RunScript(this SqlServerDatabase database, string fileName, SqlCmdRunScriptSettings? settings = null)
2528
{
29+
ArgumentNullException.ThrowIfNull(database, nameof(database));
30+
ArgumentNullException.ThrowIfNull(fileName, nameof(fileName));
31+
32+
if (!File.Exists(fileName))
33+
{
34+
throw new FileNotFoundException($"Could not find file '{fileName}'", fileName);
35+
}
36+
2637
if (settings is null)
2738
{
2839
settings = new SqlCmdRunScriptSettings();

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,5 +129,48 @@ public async Task Test2Async()
129129
// Insert a row which should not be use in other tests.
130130
await this.database.InsertIntoAsync("MyTable", new { Id = 99, Name = "Should not be here for the next test" });
131131
}
132+
133+
[Fact]
134+
public void Initialize_WithDatabaseArgumentNull()
135+
{
136+
var act = () =>
137+
{
138+
SqlCmdDatabaseInitializer.Initialize(null, default, default);
139+
};
140+
141+
act.Should().ThrowExactly<ArgumentNullException>()
142+
.WithParameterName("initializer");
143+
}
144+
145+
[Fact]
146+
public void Initialize_WithFileNameArgumentNull()
147+
{
148+
var initializer = new SqlServerDatabaseInitializer();
149+
150+
initializer.Invoking(i => i.Initialize(null, default))
151+
.Should().ThrowExactly<ArgumentNullException>()
152+
.WithParameterName("fileName");
153+
}
154+
155+
[Fact]
156+
public void Initialize_WithConnectionStringArgumentNull()
157+
{
158+
var initializer = new SqlServerDatabaseInitializer();
159+
160+
initializer.Invoking(i => i.Initialize("The file name", null))
161+
.Should().ThrowExactly<ArgumentNullException>()
162+
.WithParameterName("connectionString");
163+
}
164+
165+
[Fact]
166+
public void Initialize_WithFileNotFound()
167+
{
168+
var initializer = new SqlServerDatabaseInitializer();
169+
170+
initializer.Invoking(i => i.Initialize("C:/Directory/FileNotFound.sql", "The connection stirng"))
171+
.Should().ThrowExactly<FileNotFoundException>()
172+
.WithMessage("Could not find file 'C:/Directory/FileNotFound.sql'")
173+
.Which.FileName.Should().Be("C:/Directory/FileNotFound.sql");
174+
}
132175
}
133176
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//-----------------------------------------------------------------------
2+
// <copyright file="SqlCmdRunScriptSettingsTest.cs" company="P.O.S Informatique">
3+
// Copyright (c) P.O.S Informatique. All rights reserved.
4+
// </copyright>
5+
//-----------------------------------------------------------------------
6+
7+
namespace PosInformatique.Testing.Databases.SqlServer.Tests
8+
{
9+
public class SqlCmdRunScriptSettingsTest
10+
{
11+
[Fact]
12+
public void Constructor()
13+
{
14+
var settings = new SqlCmdRunScriptSettings();
15+
16+
settings.Variables.Should().BeEmpty();
17+
}
18+
}
19+
}

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,5 +135,38 @@ Changed database context to 'SqlCmdSqlServerExtensionsTest_RunScript_WithErros'.
135135

136136
table.Rows.Should().BeEmpty();
137137
}
138+
139+
[Fact]
140+
public void RunScript_WithDatabaseArgumentNull()
141+
{
142+
var act = () =>
143+
{
144+
SqlCmdSqlServerDatabaseExtensions.RunScript(null, default, default);
145+
};
146+
147+
act.Should().ThrowExactly<ArgumentNullException>()
148+
.WithParameterName("database");
149+
}
150+
151+
[Fact]
152+
public void RunScript_WithFileNameArgumentNull()
153+
{
154+
var server = new SqlServer(ConnectionString);
155+
156+
server.Master.Invoking(m => m.RunScript(null, default))
157+
.Should().ThrowExactly<ArgumentNullException>()
158+
.WithParameterName("fileName");
159+
}
160+
161+
[Fact]
162+
public void RunScript_WithFileNotFound()
163+
{
164+
var server = new SqlServer(ConnectionString);
165+
166+
server.Master.Invoking(m => m.RunScript("C:/Directory/FileNotFound.sql", default))
167+
.Should().ThrowExactly<FileNotFoundException>()
168+
.WithMessage("Could not find file 'C:/Directory/FileNotFound.sql'")
169+
.Which.FileName.Should().Be("C:/Directory/FileNotFound.sql");
170+
}
138171
}
139172
}

0 commit comments

Comments
 (0)