cJSON
[Ported from github.com/DaveGamble/cJSON] Fix: heap-use-after-free in merge_patch when patch is subtree of target (#1065)
Brought by drakestapleton
cJSON / tests / unity / test / spec / generate_module_existing_file_spec.rb
| 1 | |
| 2 | require '../auto/generate_module.rb' |
| 3 | require 'fileutils' |
| 4 | |
| 5 | def touch_src(file) |
| 6 | FileUtils.touch "sandbox/src/#{file}" |
| 7 | end |
| 8 | |
| 9 | def touch_test(file) |
| 10 | FileUtils.touch "sandbox/test/#{file}" |
| 11 | end |
| 12 | |
| 13 | def create_src_with_known_content(file) |
| 14 | File.open("sandbox/src/#{file}", "w") {|f| f.write("the original #{file}")} |
| 15 | end |
| 16 | |
| 17 | def create_test_with_known_content(file) |
| 18 | File.open("sandbox/test/#{file}", "w") {|f| f.write("the original #{file}")} |
| 19 | end |
| 20 | |
| 21 | def expect_src_content_didnt_change(file) |
| 22 | expect(File.read("sandbox/src/#{file}")).to eq("the original #{file}") |
| 23 | end |
| 24 | |
| 25 | def expect_test_content_didnt_change(file) |
| 26 | expect(File.read("sandbox/test/#{file}")).to eq("the original #{file}") |
| 27 | end |
| 28 | |
| 29 | def expect_src_file_to_exist(file) |
| 30 | expect(File.exist?("sandbox/src/#{file}")).to be true |
| 31 | end |
| 32 | |
| 33 | def expect_test_file_to_exist(file) |
| 34 | expect(File.exist?("sandbox/test/#{file}")).to be true |
| 35 | end |
| 36 | |
| 37 | describe "UnityModuleGenerator" do |
| 38 | |
| 39 | before do |
| 40 | # clean sandbox and setup our "project" folders |
| 41 | FileUtils.rm_rf "sandbox" |
| 42 | FileUtils.mkdir_p "sandbox" |
| 43 | FileUtils.mkdir_p "sandbox/src" |
| 44 | FileUtils.mkdir_p "sandbox/test" |
| 45 | |
| 46 | @options = { |
| 47 | :path_src => "sandbox/src", |
| 48 | :path_tst => "sandbox/test", |
| 49 | } |
| 50 | end |
| 51 | |
| 52 | context "with src pattern" do |
| 53 | before do |
| 54 | @options[:pattern] = "src" |
| 55 | end |
| 56 | |
| 57 | it "fails when all files already exist" do |
| 58 | # create an existing triad of files |
| 59 | touch_src "meh.c" |
| 60 | touch_src "meh.h" |
| 61 | touch_test "Testmeh.c" |
| 62 | expect { |
| 63 | UnityModuleGenerator.new(@options).generate("meh") |
| 64 | }.to raise_error("ERROR: File meh already exists. Exiting.") |
| 65 | end |
| 66 | |
| 67 | it "creates the test file if the source and header files exist" do |
| 68 | # Create the existing files. |
| 69 | touch_src "meh.c" |
| 70 | touch_src "meh.h" |
| 71 | |
| 72 | UnityModuleGenerator.new(@options).generate("meh") |
| 73 | |
| 74 | expect_test_file_to_exist "Testmeh.c" |
| 75 | end |
| 76 | |
| 77 | it "does not alter existing files" do |
| 78 | # Create some files with known content. |
| 79 | create_src_with_known_content "meh.c" |
| 80 | create_src_with_known_content "meh.h" |
| 81 | |
| 82 | UnityModuleGenerator.new(@options).generate("meh") |
| 83 | |
| 84 | expect_src_content_didnt_change "meh.c" |
| 85 | expect_src_content_didnt_change "meh.c" |
| 86 | end |
| 87 | |
| 88 | it "does not alter existing test files" do |
| 89 | # Create some files with known content. |
| 90 | create_test_with_known_content "Testmeh.c" |
| 91 | |
| 92 | UnityModuleGenerator.new(@options).generate("meh") |
| 93 | |
| 94 | expect_test_content_didnt_change "Testmeh.c" |
| 95 | end |
| 96 | |
| 97 | end |
| 98 | |
| 99 | context "with mch pattern" do |
| 100 | before do |
| 101 | @options[:pattern] = "mch" |
| 102 | end |
| 103 | |
| 104 | it "fails when all files exist" do |
| 105 | touch_src "meh_model.c" |
| 106 | touch_src "meh_conductor.c" |
| 107 | touch_src "meh_hardware.c" |
| 108 | touch_src "meh_model.h" |
| 109 | touch_src "meh_conductor.h" |
| 110 | touch_src "meh_hardware.h" |
| 111 | touch_test "Testmeh_model.c" |
| 112 | touch_test "Testmeh_conductor.c" |
| 113 | touch_test "Testmeh_hardware.c" |
| 114 | expect { |
| 115 | UnityModuleGenerator.new(@options).generate("meh") |
| 116 | }.to raise_error("ERROR: File meh_model already exists. Exiting.") |
| 117 | end |
| 118 | |
| 119 | it "creates files that don't exist" do |
| 120 | touch_src "meh_model.c" |
| 121 | touch_src "meh_conductor.c" |
| 122 | touch_src "meh_hardware.c" |
| 123 | touch_src "meh_model.h" |
| 124 | touch_src "meh_conductor.h" |
| 125 | |
| 126 | UnityModuleGenerator.new(@options).generate("meh") |
| 127 | |
| 128 | expect_src_file_to_exist "meh_hardware.h" |
| 129 | expect_test_file_to_exist "Testmeh_model.c" |
| 130 | expect_test_file_to_exist "Testmeh_conductor.c" |
| 131 | expect_test_file_to_exist "Testmeh_hardware.c" |
| 132 | end |
| 133 | |
| 134 | it "does not alter existing source files" do |
| 135 | create_src_with_known_content "meh_model.c" |
| 136 | create_src_with_known_content "meh_model.c" |
| 137 | create_src_with_known_content "meh_model.c" |
| 138 | create_src_with_known_content "meh_model.h" |
| 139 | create_src_with_known_content "meh_model.c" |
| 140 | |
| 141 | UnityModuleGenerator.new(@options).generate("meh") |
| 142 | |
| 143 | expect_src_content_didnt_change "meh_model.c" |
| 144 | expect_src_content_didnt_change "meh_model.c" |
| 145 | expect_src_content_didnt_change "meh_model.c" |
| 146 | expect_src_content_didnt_change "meh_model.c" |
| 147 | end |
| 148 | |
| 149 | it "does not alter existing test files" do |
| 150 | create_test_with_known_content "Testmeh_model.c" |
| 151 | |
| 152 | UnityModuleGenerator.new(@options).generate("meh") |
| 153 | |
| 154 | expect_test_content_didnt_change "Testmeh_model.c" |
| 155 | end |
| 156 | |
| 157 | end |
| 158 | end |