From eef0499f291c064b68b3883f7a7303875ce3a3ec Mon Sep 17 00:00:00 2001 From: armadavalor Date: Sun, 15 Nov 2026 23:22:16 +0300 Subject: [PATCH] [#138] Do not truncate the output database when no input files match When the input paths match no files, analyze opened the output database (truncating any existing one) before finding there was nothing to do. Check for an empty input set before the database is created. --- Analyzer/AnalyzerTool.cs | 8 ++++++++ UnityDataTool.Tests/AnalyzeExitCodeTests.cs | 22 +++++++++++++++++++++ 2 files changed, 30 insertions(+) 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() {