Files
patch/tests/test_prerequisite.cpp
Shannon Booth 6a6259c729 tests: declare compatibility per test
GNU comparison was controlled per test executable. That kept process
tests in test_unit from being compared, while enabling comparison there
would also run its unit tests twice.

Introduce COMPAT_TEST for cases that run against both sb_patch and GNU
patch. Keep PATCH_TEST for project-specific behavior and TEST for unit
coverage. Mark stable GNU differences as expected compatibility failures.
Keep host-dependent ed behavior project-only because GNU delegates those
scripts to an external editor.

Merge the normal and conditional PTY tests into one patch_tests runner
now that executable boundaries no longer control GNU comparison. Keep
test_oom separate because it runs patch_oom.
2026-07-25 09:53:53 +02:00

212 lines
3.8 KiB
C++

// SPDX-License-Identifier: BSD-3-Clause
// Copyright 2023 Shannon Booth <shannon.ml.booth@gmail.com>
#include <patch/file.h>
#include <patch/process.h>
#include <patch/pty_spawn.h>
#include <patch/test.h>
COMPAT_TEST(prerequisite_is_met)
{
{
Patch::File file("diff.patch", std::ios_base::out);
file << R"(Prereq: version-1.2.3
--- a 2023-01-18 18:46:47.745632233 +1300
+++ b 2023-01-18 18:46:53.339025728 +1300
@@ -3,5 +3,5 @@
2
3
4
-5
+4
6
)";
file.close();
}
{
Patch::File file("a", std::ios_base::out);
file << R"(version-1.2.3
1
2
3
4
5
6
)";
file.close();
}
Process process(patch_path, { patch_path, "-i", "diff.patch", nullptr });
EXPECT_EQ(process.stdout_data(), "patching file a\n");
EXPECT_EQ(process.stderr_data(), "");
EXPECT_EQ(process.return_code(), 0);
EXPECT_FILE_EQ("a", R"(version-1.2.3
1
2
3
4
4
6
)");
}
COMPAT_TEST(prerequisite_is_not_met_continue_success)
{
{
Patch::File file("diff.patch", std::ios_base::out);
file << R"(Prereq: version-1.2.4
--- a 2023-01-18 18:46:47.745632233 +1300
+++ b 2023-01-18 18:46:53.339025728 +1300
@@ -3,5 +3,5 @@
2
3
4
-5
+4
6
)";
file.close();
}
{
Patch::File file("a", std::ios_base::out);
file << R"(version-1.2.3
1
2
3
4
5
6
)";
file.close();
}
PtySpawn term(patch_path, { patch_path, "-i", "diff.patch", nullptr }, "y\n");
EXPECT_EQ(term.output(), "This file doesn't appear to be the version-1.2.4 version -- patch anyway? [n] patching file a\n");
EXPECT_EQ(term.return_code(), 0);
}
COMPAT_TEST(prerequisite_is_not_met_abort)
{
{
Patch::File file("diff.patch", std::ios_base::out);
file << R"(Prereq: version-1.2.4
--- a 2023-01-18 18:46:47.745632233 +1300
+++ b 2023-01-18 18:46:53.339025728 +1300
@@ -3,5 +3,5 @@
2
3
4
-5
+4
6
)";
file.close();
}
{
Patch::File file("a", std::ios_base::out);
file << R"(version-1.2.3
1
2
3
4
5
6
)";
file.close();
}
PtySpawn term(patch_path, { patch_path, "-i", "diff.patch", nullptr }, "\n");
EXPECT_EQ(term.output(), "This file doesn't appear to be the version-1.2.4 version -- patch anyway? [n] " + std::string(patch_path) + ": **** aborted\n");
EXPECT_EQ(term.return_code(), 2);
}
COMPAT_TEST(prerequisite_is_not_met_force)
{
{
Patch::File file("diff.patch", std::ios_base::out);
file << R"(Prereq: version-1.2.4
--- a 2023-01-18 18:46:47.745632233 +1300
+++ b 2023-01-18 18:46:53.339025728 +1300
@@ -3,5 +3,5 @@
2
3
4
-5
+4
6
)";
file.close();
}
{
Patch::File file("a", std::ios_base::out);
file << R"(version-1.2.3
1
2
3
4
5
6
)";
file.close();
}
Process process(patch_path, { patch_path, "-i", "diff.patch", "--force", nullptr });
EXPECT_EQ(process.stdout_data(), "Warning: this file doesn't appear to be the version-1.2.4 version -- patching anyway.\npatching file a\n");
EXPECT_EQ(process.stderr_data(), "");
EXPECT_EQ(process.return_code(), 0);
}
COMPAT_TEST(prerequisite_is_not_met_batch)
{
{
Patch::File file("diff.patch", std::ios_base::out);
file << R"(Prereq: version-1.2.4
--- a 2023-01-18 18:46:47.745632233 +1300
+++ b 2023-01-18 18:46:53.339025728 +1300
@@ -3,5 +3,5 @@
2
3
4
-5
+4
6
)";
file.close();
}
{
Patch::File file("a", std::ios_base::out);
file << R"(version-1.2.3
1
2
3
4
5
6
)";
file.close();
}
Process process(patch_path, { patch_path, "-i", "diff.patch", "--batch", nullptr });
EXPECT_EQ(process.stderr_data(), std::string(patch_path) + ": **** This file doesn't appear to be the version-1.2.4 version -- aborting.\n");
EXPECT_EQ(process.stdout_data(), "");
EXPECT_EQ(process.return_code(), 2);
}