Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Analyzer/AnalyzerTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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).");
Expand Down
22 changes: 22 additions & 0 deletions UnityDataTool.Tests/AnalyzeExitCodeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
Loading