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
2 changes: 1 addition & 1 deletion .github/workflows/msvc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:

# Upload SARIF file as an Artifact to download and view
- name: Upload SARIF as an Artifact
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: sarif-file
path: ${{ steps.run-analysis.outputs.sarif }}
59 changes: 0 additions & 59 deletions .github/workflows/sonar.yml

This file was deleted.

8 changes: 5 additions & 3 deletions src/applier.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright 2022-2024 Shannon Booth <shannon.ml.booth@gmail.com>
// Copyright 2022-2026 Shannon Booth <shannon.ml.booth@gmail.com>

#include <istream>
#include <limits>
Expand Down Expand Up @@ -117,8 +117,10 @@ static LineNumber write_define_hunk(LineWriter& output, const Hunk& hunk, const
}
}

if (define_state != DefineState::Outside)
output << "#endif" << lines.at(lines.size() - 1).newline;
if (define_state != DefineState::Outside) {
const auto newline = lines.empty() ? NewLine::LF : lines.at(lines.size() - 1).newline;
output << "#endif" << newline;
}

return static_cast<LineNumber>(line_number);
}
Expand Down
59 changes: 56 additions & 3 deletions tests/lib/include/patch/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
#pragma once

#include <functional>
#include <iomanip>
#include <iostream>
#include <patch/file.h>
#include <sstream>
#include <stdexcept>
#include <string>
#include <type_traits>
Expand All @@ -28,6 +30,54 @@ void test_error_format(const T& a)
std::cerr << static_cast<typename std::underlying_type<T>::type>(a);
}

inline std::string escaped_string_for_test_output(const std::string& value)
{
std::ostringstream out;
out << '"';

for (unsigned char c : value) {
switch (c) {
case '\\':
out << "\\\\";
break;
case '"':
out << "\\\"";
break;
case '\n':
out << "\\n";
break;
case '\r':
out << "\\r";
break;
case '\t':
out << "\\t";
break;
default:
if (c >= 0x20 && c < 0x7f) {
out << static_cast<char>(c);
} else {
out << "\\x" << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(c) << std::dec;
}
break;
}
}

out << '"';
return out.str();
}

template<typename A, typename B>
void maybe_print_string_diff_details(const A&, const B&)
{
}

inline void maybe_print_string_diff_details(const std::string& lhs, const std::string& rhs)
{
std::cerr << " (lhs len=" << lhs.size() << ", rhs len=" << rhs.size() << ")";
std::cerr << "\n lhs escaped: " << ::Patch::escaped_string_for_test_output(lhs);
std::cerr << "\n rhs escaped: " << ::Patch::escaped_string_for_test_output(rhs);
}

class test_assertion_failure : public std::runtime_error {
public:
using runtime_error::runtime_error;
Expand Down Expand Up @@ -66,12 +116,15 @@ class test_assertion_failure : public std::runtime_error {
#define EXPECT_EQ(lhs, rhs) \
do { \
/* NOLINTNEXTLINE(readability-container-size-empty) */ \
if ((lhs) != (rhs)) { \
const auto patch_lhs = (lhs); \
const auto patch_rhs = (rhs); \
if (patch_lhs != patch_rhs) { \
std::cerr << "FAIL: " __FILE__ << ":" << __LINE__ << ": "; \
std::cerr << "'EXPECT_EQ(" << #lhs << ", " << #rhs << ")' failed, "; \
Patch::test_error_format(lhs); \
Patch::test_error_format(patch_lhs); \
std::cerr << " != "; \
Patch::test_error_format(rhs); \
Patch::test_error_format(patch_rhs); \
Patch::maybe_print_string_diff_details(patch_lhs, patch_rhs); \
std::cerr << '\n'; \
throw Patch::test_assertion_failure("Test failed"); \
} \
Expand Down
25 changes: 25 additions & 0 deletions tests/test_defines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,28 @@ int main()
}
)");
}

PATCH_TEST(defines_add_file_from_empty_input)
{
{
Patch::File file("diff.patch", std::ios_base::out);
file << R"(--- /dev/null 2022-01-30 13:57:31.173528027 +1300
+++ added.cpp 2022-01-30 13:57:36.321216497 +1300
@@ -0,0 +1,3 @@
+int main()
+{
+}
)";
}

Process process(patch_path, { patch_path, "-i", "diff.patch", "--ifdef", "TEST_PATCH", nullptr });
EXPECT_EQ(process.stdout_data(), "patching file added.cpp\n");
EXPECT_EQ(process.stderr_data(), "");
EXPECT_EQ(process.return_code(), 0);
EXPECT_FILE_EQ("added.cpp", R"(#ifdef TEST_PATCH
int main()
{
}
#endif
)");
}
Loading