generate_module_existing_file_spec.rb 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. require '../auto/generate_module.rb'
  2. require 'fileutils'
  3. def touch_src(file)
  4. FileUtils.touch "sandbox/src/#{file}"
  5. end
  6. def touch_test(file)
  7. FileUtils.touch "sandbox/test/#{file}"
  8. end
  9. def create_src_with_known_content(file)
  10. File.open("sandbox/src/#{file}", "w") {|f| f.write("the original #{file}")}
  11. end
  12. def create_test_with_known_content(file)
  13. File.open("sandbox/test/#{file}", "w") {|f| f.write("the original #{file}")}
  14. end
  15. def expect_src_content_didnt_change(file)
  16. expect(File.read("sandbox/src/#{file}")).to eq("the original #{file}")
  17. end
  18. def expect_test_content_didnt_change(file)
  19. expect(File.read("sandbox/test/#{file}")).to eq("the original #{file}")
  20. end
  21. def expect_src_file_to_exist(file)
  22. expect(File.exist?("sandbox/src/#{file}")).to be true
  23. end
  24. def expect_test_file_to_exist(file)
  25. expect(File.exist?("sandbox/test/#{file}")).to be true
  26. end
  27. describe "UnityModuleGenerator" do
  28. before do
  29. # clean sandbox and setup our "project" folders
  30. FileUtils.rm_rf "sandbox"
  31. FileUtils.mkdir_p "sandbox"
  32. FileUtils.mkdir_p "sandbox/src"
  33. FileUtils.mkdir_p "sandbox/test"
  34. @options = {
  35. :path_src => "sandbox/src",
  36. :path_tst => "sandbox/test",
  37. }
  38. end
  39. context "with src pattern" do
  40. before do
  41. @options[:pattern] = "src"
  42. end
  43. it "fails when all files already exist" do
  44. # create an existing triad of files
  45. touch_src "meh.c"
  46. touch_src "meh.h"
  47. touch_test "Testmeh.c"
  48. expect {
  49. UnityModuleGenerator.new(@options).generate("meh")
  50. }.to raise_error("ERROR: File meh already exists. Exiting.")
  51. end
  52. it "creates the test file if the source and header files exist" do
  53. # Create the existing files.
  54. touch_src "meh.c"
  55. touch_src "meh.h"
  56. UnityModuleGenerator.new(@options).generate("meh")
  57. expect_test_file_to_exist "Testmeh.c"
  58. end
  59. it "does not alter existing files" do
  60. # Create some files with known content.
  61. create_src_with_known_content "meh.c"
  62. create_src_with_known_content "meh.h"
  63. UnityModuleGenerator.new(@options).generate("meh")
  64. expect_src_content_didnt_change "meh.c"
  65. expect_src_content_didnt_change "meh.c"
  66. end
  67. it "does not alter existing test files" do
  68. # Create some files with known content.
  69. create_test_with_known_content "Testmeh.c"
  70. UnityModuleGenerator.new(@options).generate("meh")
  71. expect_test_content_didnt_change "Testmeh.c"
  72. end
  73. end
  74. context "with mch pattern" do
  75. before do
  76. @options[:pattern] = "mch"
  77. end
  78. it "fails when all files exist" do
  79. touch_src "meh_model.c"
  80. touch_src "meh_conductor.c"
  81. touch_src "meh_hardware.c"
  82. touch_src "meh_model.h"
  83. touch_src "meh_conductor.h"
  84. touch_src "meh_hardware.h"
  85. touch_test "Testmeh_model.c"
  86. touch_test "Testmeh_conductor.c"
  87. touch_test "Testmeh_hardware.c"
  88. expect {
  89. UnityModuleGenerator.new(@options).generate("meh")
  90. }.to raise_error("ERROR: File meh_model already exists. Exiting.")
  91. end
  92. it "creates files that don't exist" do
  93. touch_src "meh_model.c"
  94. touch_src "meh_conductor.c"
  95. touch_src "meh_hardware.c"
  96. touch_src "meh_model.h"
  97. touch_src "meh_conductor.h"
  98. UnityModuleGenerator.new(@options).generate("meh")
  99. expect_src_file_to_exist "meh_hardware.h"
  100. expect_test_file_to_exist "Testmeh_model.c"
  101. expect_test_file_to_exist "Testmeh_conductor.c"
  102. expect_test_file_to_exist "Testmeh_hardware.c"
  103. end
  104. it "does not alter existing source files" do
  105. create_src_with_known_content "meh_model.c"
  106. create_src_with_known_content "meh_model.c"
  107. create_src_with_known_content "meh_model.c"
  108. create_src_with_known_content "meh_model.h"
  109. create_src_with_known_content "meh_model.c"
  110. UnityModuleGenerator.new(@options).generate("meh")
  111. expect_src_content_didnt_change "meh_model.c"
  112. expect_src_content_didnt_change "meh_model.c"
  113. expect_src_content_didnt_change "meh_model.c"
  114. expect_src_content_didnt_change "meh_model.c"
  115. end
  116. it "does not alter existing test files" do
  117. create_test_with_known_content "Testmeh_model.c"
  118. UnityModuleGenerator.new(@options).generate("meh")
  119. expect_test_content_didnt_change "Testmeh_model.c"
  120. end
  121. end
  122. end