diff --git a/src/openrct2/CmdlineSprite.cpp b/src/openrct2/CmdlineSprite.cpp index bb97194498..9c4a7daa9d 100644 --- a/src/openrct2/CmdlineSprite.cpp +++ b/src/openrct2/CmdlineSprite.cpp @@ -589,10 +589,11 @@ int32_t cmdline_for_sprite(const char** argv, int32_t argc) bool silent = (argc >= 4 && strcmp(argv[3], "silent") == 0); - SpriteFile baseFile; - baseFile.Header.num_entries = 0; - baseFile.Header.total_size = 0; - baseFile.Save(spriteFilePath); + // keep sprite file entirely in memory until ready to write out a complete, + // correct file + SpriteFile spriteFile; + spriteFile.Header.num_entries = 0; + spriteFile.Header.total_size = 0; fprintf(stdout, "Building: %s\n", spriteFilePath); @@ -632,25 +633,18 @@ int32_t cmdline_for_sprite(const char** argv, int32_t argc) return -1; } - auto spriteFile = SpriteFile::Open(spriteFilePath); - if (!spriteFile.has_value()) - { - fprintf(stderr, "Unable to open sprite file: %s\nCanceling\n", spriteFilePath); - return -1; - } - - spriteFile->AddImage(importResult.value()); - - if (!spriteFile->Save(spriteFilePath)) - { - fprintf(stderr, "Could not save sprite file: %s\nCanceling\n", imagePath.c_str()); - return -1; - } + spriteFile.AddImage(importResult.value()); if (!silent) fprintf(stdout, "Added: %s\n", imagePath.c_str()); } + if (!spriteFile.Save(spriteFilePath)) + { + log_error("Could not save sprite file, cancelling."); + return -1; + } + free(directoryPath); fprintf(stdout, "Finished\n"); diff --git a/test/tests/CLITests.cpp b/test/tests/CLITests.cpp new file mode 100644 index 0000000000..92be6408f1 --- /dev/null +++ b/test/tests/CLITests.cpp @@ -0,0 +1,98 @@ + +#include "TestData.h" + +#include +#include +#include +#include +#include +#include + +class CommandLineTests : public testing::Test +{ +public: + static std::string SpriteTestDataPath() + { + return Path::Combine(TestData::GetBasePath(), "sprites"); + } + + static std::string ManifestFilePath() + { + return Path::Combine(SpriteTestDataPath(), "manifest.json"); + } + + static std::string BadManifestFilePath() + { + return Path::Combine(SpriteTestDataPath(), "badManifest.json"); + } + + static std::string ExampleSpriteFilePath() + { + return Path::Combine(SpriteTestDataPath(), "example.dat"); + } + + static std::string BuildOutputfilePath() + { + return Path::Combine(SpriteTestDataPath(), "result.dat"); + } + + static bool CompareSpriteFiles(std::string original, std::string generated) + { + std::ifstream originalFile(original, std::ios::binary | std::ifstream::in); + std::ifstream generatedFile(generated, std::ios::binary | std::ifstream::in); + if (!(originalFile.is_open() && generatedFile.is_open())) + { + return false; + } + if (originalFile.tellg() != generatedFile.tellg()) + { + return false; + } + return std::equal( + std::istreambuf_iterator(originalFile.rdbuf()), std::istreambuf_iterator(), + std::istreambuf_iterator(generatedFile.rdbuf())); + } +}; + +TEST_F(CommandLineTests, cmdline_cmdline_for_sprite_details) +{ + std::string exampleFilePath = ExampleSpriteFilePath(); + const char* detailsCmd[3] = { "details", exampleFilePath.c_str() }; + + int32_t result = cmdline_for_sprite(detailsCmd, 2); + // need to come up with some way to extract stdout/stderr stream if we want to + // fully test this module + ASSERT_EQ(result, 1); +} + +TEST_F(CommandLineTests, cmdline_cmdline_for_sprite_build) +{ + std::string manifestFilePath = ManifestFilePath(); + std::string outputfilePath = BuildOutputfilePath(); + const char* detailsCmd[3] = { "build", outputfilePath.c_str(), manifestFilePath.c_str() }; + + int32_t result = cmdline_for_sprite(detailsCmd, 3); + ASSERT_EQ(result, 1); + // compare the resulting output file and assert its identical to expected + ASSERT_TRUE(CompareSpriteFiles(ExampleSpriteFilePath(), outputfilePath)); +} + +TEST_F(CommandLineTests, cmdline_cmdline_for_sprite_failed_build) +{ + // run on correct manifest file + std::string manifestFilePath = ManifestFilePath(); + std::string outputfilePath = BuildOutputfilePath(); + const char* detailsCmd[3] = { "build", outputfilePath.c_str(), manifestFilePath.c_str() }; + int32_t result = cmdline_for_sprite(detailsCmd, 3); + ASSERT_EQ(result, 1); + ASSERT_TRUE(CompareSpriteFiles(ExampleSpriteFilePath(), outputfilePath)); + + // now use bad manifest and make sure output file is not edited + std::string badManifestFilePath = BadManifestFilePath(); + detailsCmd[2] = badManifestFilePath.c_str(); + result = cmdline_for_sprite(detailsCmd, 3); + // check the command failed + ASSERT_EQ(result, -1); + // validate the target file was unchanged + ASSERT_TRUE(CompareSpriteFiles(ExampleSpriteFilePath(), outputfilePath)); +} diff --git a/test/tests/testdata/sprites/badManifest.json b/test/tests/testdata/sprites/badManifest.json new file mode 100644 index 0000000000..b3e46d3469 --- /dev/null +++ b/test/tests/testdata/sprites/badManifest.json @@ -0,0 +1,8 @@ +[ + { + "path": "../images/logo.png" + }, + { + "path": "../images/FAKEFILE.png" + } +] diff --git a/test/tests/testdata/sprites/example.dat b/test/tests/testdata/sprites/example.dat new file mode 100644 index 0000000000..c81bf87a42 Binary files /dev/null and b/test/tests/testdata/sprites/example.dat differ diff --git a/test/tests/testdata/sprites/manifest.json b/test/tests/testdata/sprites/manifest.json new file mode 100644 index 0000000000..5af8f76b68 --- /dev/null +++ b/test/tests/testdata/sprites/manifest.json @@ -0,0 +1,5 @@ +[ + { + "path": "../images/logo.png" + } +] diff --git a/test/tests/testdata/sprites/result.dat b/test/tests/testdata/sprites/result.dat new file mode 100644 index 0000000000..1b1cb4d44c Binary files /dev/null and b/test/tests/testdata/sprites/result.dat differ diff --git a/test/tests/tests.vcxproj b/test/tests/tests.vcxproj index 574ac7a22b..d192c57318 100644 --- a/test/tests/tests.vcxproj +++ b/test/tests/tests.vcxproj @@ -57,6 +57,7 @@ + @@ -78,5 +79,10 @@ + + + + + \ No newline at end of file