From c483bcc8e415714f57f38d767eb80f27b649240b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Foidl?= Date: Sun, 16 Dec 2018 20:13:25 +0100 Subject: [PATCH] Allows the file to be in PWD (#29) * Reproduced the bug in the CI * Added unit tests for the issue * Fixed "Path cannot be the empty string or all whitespace." --- .circleci/config.yml | 22 ++++++ source/trx2junit/Worker.cs | 4 +- .../WorkerTests/RunAsync/LocationIsPwd.cs | 74 +++++++++++++++++++ 3 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 tests/trx2junit.Tests/WorkerTests/RunAsync/LocationIsPwd.cs diff --git a/.circleci/config.yml b/.circleci/config.yml index 10b6baa..042d84d 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -71,6 +71,28 @@ jobs: echo "" ./verify-xml.sh "TestResults/nunit.xml" ./verify-xml.sh "TestResults/mstest.xml" + - run: + name: run-single-arg + command: | + mkdir ./single-arg + cp tests/trx2junit.Tests/data/nunit.trx ./single-arg + + echo "-----------------------------------------------" + echo "file in different location than pwd" + trx2junit ./single-arg/nunit.trx + + echo "" + ./verify-xml.sh "single-arg/nunit.xml" + + echo "" + echo "-----------------------------------------------" + echo "file in same location than pwd" + cd single-arg + trx2junit nunit.trx + + echo "" + cd - + ./verify-xml.sh "single-arg/nunit.xml" - run: name: run-multiple-args command: | diff --git a/source/trx2junit/Worker.cs b/source/trx2junit/Worker.cs index b9fba5f..5d83845 100644 --- a/source/trx2junit/Worker.cs +++ b/source/trx2junit/Worker.cs @@ -58,7 +58,9 @@ internal static string GetJunitFile(string trxFile, string outputPath = null) private static void EnsureOutputDirectoryExists(string jUnitFile) { string directory = Path.GetDirectoryName(jUnitFile); - Directory.CreateDirectory(directory); + + if (!string.IsNullOrWhiteSpace(directory)) + Directory.CreateDirectory(directory); } } } diff --git a/tests/trx2junit.Tests/WorkerTests/RunAsync/LocationIsPwd.cs b/tests/trx2junit.Tests/WorkerTests/RunAsync/LocationIsPwd.cs new file mode 100644 index 0000000..8690b6f --- /dev/null +++ b/tests/trx2junit.Tests/WorkerTests/RunAsync/LocationIsPwd.cs @@ -0,0 +1,74 @@ +using System; +using System.IO; +using System.Threading.Tasks; +using NUnit.Framework; + +namespace trx2junit.Tests.WorkerTests.RunAsync +{ + [TestFixture] + public class LocationIsPwd + { + private string _curDir; + //--------------------------------------------------------------------- + [SetUp] + public void SetUp() + { + _curDir = Environment.CurrentDirectory; + Environment.CurrentDirectory = "./data"; + } + //--------------------------------------------------------------------- + [TearDown] + public void TearDown() + { + Environment.CurrentDirectory = _curDir; + } + //--------------------------------------------------------------------- + [Test] + public async Task Single_file_given___converted() + { + string[] expectedFiles = { "nunit.xml" }; + DeleteExpectedFiles(expectedFiles); + + var sut = new Worker(); + + string[] args = { "nunit.trx" }; + var options = new WorkerOptions(args); + await sut.RunAsync(options); + + CheckExpectedFilesExist(expectedFiles); + } + //--------------------------------------------------------------------- + [Test] + public async Task Multiple_files_given___converted() + { + string[] expectedFiles = { "nunit.xml", "mstest.xml", "mstest-warning.xml" }; + DeleteExpectedFiles(expectedFiles); + + var sut = new Worker(); + + string[] args = { "nunit.trx", "mstest.trx", "mstest-warning.trx" }; + var options = new WorkerOptions(args); + await sut.RunAsync(options); + + CheckExpectedFilesExist(expectedFiles); + } + //--------------------------------------------------------------------- + private static void DeleteExpectedFiles(string[] files) + { + foreach (string file in files) + File.Delete(file); + } + //--------------------------------------------------------------------- + private static void CheckExpectedFilesExist(string[] files) + { + Assert.Multiple(() => + { + foreach (string file in files) + { + bool actual = File.Exists(file); + Assert.IsTrue(actual, $"File '{file}' does not exist"); + } + }); + } + } +}