|
35 | 35 | #include <algorithm> |
36 | 36 | #include <cstdio> |
37 | 37 | #include <cstdlib> |
| 38 | +#include <fstream> |
38 | 39 | #include <list> |
39 | 40 | #include <string> |
40 | 41 | #include <unordered_set> |
@@ -100,6 +101,13 @@ class TestCppcheck : public TestFixture { |
100 | 101 | TEST_CASE(getDumpFileContentsLibrary); |
101 | 102 | TEST_CASE(checkPlistOutput); |
102 | 103 | TEST_CASE(premiumResultsCache); |
| 104 | + TEST_CASE(addonScriptHash); |
| 105 | + TEST_CASE(addonJsonHash); |
| 106 | + TEST_CASE(addonArgsHash); |
| 107 | + TEST_CASE(addonQuotedArgsHash); |
| 108 | +#ifndef _WIN32 |
| 109 | + TEST_CASE(addonSingleQuotedArgsHash); |
| 110 | +#endif |
103 | 111 | TEST_CASE(purgedConfiguration); |
104 | 112 | TEST_CASE(recheckInclude); |
105 | 113 | } |
@@ -612,6 +620,77 @@ class TestCppcheck : public TestFixture { |
612 | 620 | ASSERT(hash1 != hash2); |
613 | 621 | } |
614 | 622 |
|
| 623 | + void addonHashInternal(AddonInfo addon, const ScopedFile& file) const |
| 624 | + { |
| 625 | + Settings settings; |
| 626 | + Suppressions supprs; |
| 627 | + ErrorLogger2 errorLogger; |
| 628 | + |
| 629 | + std::vector<std::string> files; |
| 630 | + |
| 631 | + const char code[] = "void f();\nint x;\n"; |
| 632 | + simplecpp::TokenList tokens(code, files, "m1.c"); |
| 633 | + |
| 634 | + Preprocessor preprocessor(tokens, settings, errorLogger, Standards::Language::C); |
| 635 | + ASSERT(preprocessor.loadAllIncludes(files)); |
| 636 | + |
| 637 | + addon.name = "testaddon"; |
| 638 | + settings.addons.insert(addon.name); |
| 639 | + settings.addonInfos.push_back(std::move(addon)); |
| 640 | + settings.addonInfos.back().initContentHash(); |
| 641 | + |
| 642 | + CppCheck check(settings, supprs, errorLogger, nullptr, false, {}); |
| 643 | + const size_t hash1 = check.calculateHash(preprocessor); |
| 644 | + |
| 645 | + settings.addonInfos.back().initContentHash(); |
| 646 | + ASSERT_EQUALS(hash1, check.calculateHash(preprocessor)); |
| 647 | + |
| 648 | + { |
| 649 | + std::ofstream of(file.path()); |
| 650 | + of << "changed content\n"; |
| 651 | + } |
| 652 | + settings.addonInfos.back().initContentHash(); |
| 653 | + // cppcheck-suppress knownConditionTrueFalse |
| 654 | + ASSERT(hash1 != check.calculateHash(preprocessor)); |
| 655 | + } |
| 656 | + |
| 657 | + void addonScriptHash() const { |
| 658 | + ScopedFile addon_script("testaddon.py", "# addon v1\n"); |
| 659 | + AddonInfo addon; |
| 660 | + addon.scriptFile = addon_script.path(); |
| 661 | + addonHashInternal(addon, addon_script); |
| 662 | + } |
| 663 | + |
| 664 | + void addonJsonHash() const { |
| 665 | + ScopedFile addon_config("testaddon.json", "{ \"script\": \"x.py\" }\n"); |
| 666 | + AddonInfo addon; |
| 667 | + addon.jsonFile = addon_config.path(); |
| 668 | + addonHashInternal(addon, addon_config); |
| 669 | + } |
| 670 | + |
| 671 | + void addonArgsHash() const { |
| 672 | + ScopedFile rule_texts("testrules.txt", "rule v1\n"); |
| 673 | + AddonInfo addon; |
| 674 | + addon.args = "--rule-texts=" + rule_texts.path(); |
| 675 | + addonHashInternal(addon, rule_texts); |
| 676 | + } |
| 677 | + |
| 678 | + void addonQuotedArgsHash() const { |
| 679 | + ScopedFile rule_texts("test rules.txt", "rule v1\n"); |
| 680 | + AddonInfo addon; |
| 681 | + addon.args = "--rule-texts=\"" + rule_texts.path() + "\""; |
| 682 | + addonHashInternal(addon, rule_texts); |
| 683 | + } |
| 684 | + |
| 685 | +#ifndef _WIN32 |
| 686 | + void addonSingleQuotedArgsHash() const { |
| 687 | + ScopedFile rule_texts("test rules.txt", "rule v1\n"); |
| 688 | + AddonInfo addon; |
| 689 | + addon.args = "--rule-texts='" + rule_texts.path() + "'"; |
| 690 | + addonHashInternal(addon, rule_texts); |
| 691 | + } |
| 692 | +#endif |
| 693 | + |
615 | 694 | void purgedConfiguration() const |
616 | 695 | { |
617 | 696 | ScopedFile test_file("test.cpp", |
|
0 commit comments