Skip to content

Commit

Permalink
Add --cc-cobertura option
Browse files Browse the repository at this point in the history
  • Loading branch information
cagrin committed Apr 16, 2024
1 parent 7b58696 commit 1703608
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ Options:
-i, --image <image> Docker image.
-p, --project <project> Database project.
-c, --collation <collation> Server collation.
-r, --result <result> Save result in JUnit XML file.
-r, --result <file> Save result to JUnit XML file.
--cc-cobertura <file> Save code coverage to Cobertura XML file.
--cc-disable Disable code coverage.
--cc-include-tsqlt Include code coverage of tSQLt schema.
-?, -h, --help Show help and usage information
Expand Down
40 changes: 40 additions & 0 deletions SqlTest.Tests/DatabaseTests/OkDatabaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,44 @@ public void InvokeSqlTestRunAllOkWithXmlResult(string image)

Assert.That.IsLike(xml, pattern);
}

[TestMethod]
[DynamicData(nameof(Images))]
public void InvokeSqlTestRunAllOkWithXmlCobertura(string image)
{
var filename = $"cobertura.xml";
_ = SystemConsole.Invoke($"dotnet SqlTest.dll runall --image {image} --project {this.Folder}/Ok --cc-cobertura {filename}");

using var str = new StreamReader(filename);
string xml = str.ReadToEnd();

string pattern = """
<?xml version="1.0"?>
<!--DOCTYPE coverage SYSTEM "http://cobertura.sourceforge.net/xml/coverage-03.dtd"-->
<coverage lines-valid="5" lines-covered="3" line-rate="0.6" version="1.9" timestamp="__________.%">
<packages>
<package name="sql">
<classes>
<class name="[[]dbo].[[]Assertions]" filename="[[]dbo].[[]Assertions]" lines-valid="3" lines-covered="3" line-rate="1" >
<methods/>
<lines>
<line number="5" hits="1" branch="false" />
<line number="7" hits="1" branch="false" />
<line number="9" hits="1" branch="false" />
</lines>
</class>
<class name="[[]dbo].[[]Example]" filename="[[]dbo].[[]Example]" lines-valid="2" lines-covered="0" line-rate="0" >
<methods/>
<lines>
</lines>
</class>

</classes>
</package>
</packages>
</coverage>%
""";

Assert.That.IsLike(xml, pattern);
}
}
3 changes: 2 additions & 1 deletion SqlTest.Tests/UnitTests/RunAllCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ SqlTest runall [options]
-i, --image <image> Docker image.
-p, --project <project> Database project.
-c, --collation <collation> Server collation.
-r, --result <result> Save result in JUnit XML file.
-r, --result <file> Save result to JUnit XML file.
--cc-cobertura <file> Save code coverage to Cobertura XML file.
--cc-disable Disable code coverage.
--cc-include-tsqlt Include code coverage of tSQLt schema.
-?, -h, --help Show help and usage information
Expand Down
3 changes: 2 additions & 1 deletion SqlTest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public static int Main(string[] args)
new Option<string>(new[] { "--image", "-i" }, "Docker image."),
new Option<string>(new[] { "--project", "-p" }, "Database project."),
new Option<string>(new[] { "--collation", "-c" }, "Server collation."),
new Option<string>(new[] { "--result", "-r" }, "Save result in JUnit XML file."),
new Option<string>(new[] { "--result", "-r" }, "Save result to JUnit XML file.") { ArgumentHelpName = "file" },
new Option<string>(new[] { "--cc-cobertura" }, "Save code coverage to Cobertura XML file.") { ArgumentHelpName = "file" },
new Option<bool>(new[] { "--cc-disable" }, "Disable code coverage."),
new Option<bool>(new[] { "--cc-include-tsqlt" }, "Include code coverage of tSQLt schema."),
};
Expand Down
13 changes: 13 additions & 0 deletions SqlTest/RunAllCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ private void RunTests()

this.code = this.coverage.Stop();

if (!string.IsNullOrEmpty(this.options.CcCobertura))
{
this.CoberturaXml();
}

stopwatchLog.Stop();
}
}
Expand Down Expand Up @@ -233,4 +238,12 @@ private void ResultXml()
string xml = con.Query<string>(sql, CommandType.StoredProcedure).First();
file.Write(xml);
}

private void CoberturaXml()
{
using var file = new StreamWriter(this.options.CcCobertura);

string xml = this.code!.Cobertura();
file.Write(xml);
}
}
2 changes: 2 additions & 0 deletions SqlTest/RunAllOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class RunAllOptions

public string Result { get; set; } = default!;

public string CcCobertura { get; set; } = default!;

public bool CcDisable { get; set; }

public bool CcIncludeTsqlt { get; set; }
Expand Down

0 comments on commit 1703608

Please sign in to comment.