diff --git a/Analyzer/AnalyzerTool.cs b/Analyzer/AnalyzerTool.cs index e416629..c2bb39c 100644 --- a/Analyzer/AnalyzerTool.cs +++ b/Analyzer/AnalyzerTool.cs @@ -63,6 +63,14 @@ public int Analyze(AnalyzeOptions options) return 1; } + // Fail before the database is opened, so an existing database at the output path is not + // truncated by a run that has nothing to analyze (issue #138). + if (files.Count == 0) + { + Console.Error.WriteLine("Error: the input paths matched no files to analyze."); + return 1; + } + if (File.Exists(m_Options.DatabaseName)) { Console.Error.WriteLine($"Overwriting existing database \"{m_Options.DatabaseName}\" (previous contents are discarded)."); diff --git a/UnityDataTool.Tests/AnalyzeExitCodeTests.cs b/UnityDataTool.Tests/AnalyzeExitCodeTests.cs index 458c438..baa9ddc 100644 --- a/UnityDataTool.Tests/AnalyzeExitCodeTests.cs +++ b/UnityDataTool.Tests/AnalyzeExitCodeTests.cs @@ -79,6 +79,28 @@ public async Task Analyze_NothingAnalyzed_RemovesDatabaseOfPreviousRun() Assert.That(File.Exists(databasePath), Is.False, "Expected the overwritten database to be removed, not left empty"); } + // An input that matches no files must not touch the database a previous run left at the + // output path (issue #138). + [Test] + public async Task Analyze_NoInputFiles_KeepsDatabaseOfPreviousRun() + { + var databasePath = SQLTestHelper.GetDatabasePath(m_TestOutputFolder); + + var (firstExitCode, _) = await RunAnalyze(m_AssetBundlePath, "-o", databasePath); + Assert.AreEqual(0, firstExitCode); + var previousContents = File.ReadAllBytes(databasePath); + + var emptyFolder = Path.Combine(m_TestOutputFolder, "empty_input"); + Directory.CreateDirectory(emptyFolder); + var (exitCode, output) = await RunAnalyze(emptyFolder, "-o", databasePath); + + Assert.AreEqual(1, exitCode); + StringAssert.Contains("the input paths matched no files to analyze", output); + StringAssert.DoesNotContain("Overwriting existing database", output); + Assert.That(File.Exists(databasePath), Is.True); + CollectionAssert.AreEqual(previousContents, File.ReadAllBytes(databasePath)); + } + [Test] public async Task Analyze_SomeFilesFailed_SucceedsAndKeepsDatabase() {