/chef/spec/unit/util/file_edit_spec.rb

https://github.com/tobiaskaestner/chef · Ruby · 135 lines · 106 code · 12 blank · 17 comment · 6 complexity · 5d0cd2fcae9728cb8d648be7a45709ad MD5 · raw file

  1. #
  2. # Author:: Nuo Yan (<nuo@opscode.com>)
  3. # Copyright:: Copyright (c) 2008 Opscode, Inc.
  4. # License:: Apache License, Version 2.0
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
  19. describe Chef::Util::FileEdit do
  20. before(:each) do
  21. @hosts_content=<<-HOSTS
  22. 127.0.0.1 localhost
  23. 255.255.255.255 broadcasthost
  24. ::1 localhost
  25. fe80::1%lo0 localhost
  26. HOSTS
  27. @tempfile = Tempfile.open('file_edit_spec')
  28. @tempfile.write(@hosts_content)
  29. @tempfile.close
  30. @fedit = Chef::Util::FileEdit.new(@tempfile.path)
  31. end
  32. after(:each) do
  33. @tempfile && @tempfile.close!
  34. end
  35. describe "initialiize" do
  36. it "should create a new Chef::Util::FileEdit object" do
  37. Chef::Util::FileEdit.new(@tempfile.path).should be_kind_of(Chef::Util::FileEdit)
  38. end
  39. it "should throw an exception if the input file does not exist" do
  40. lambda{Chef::Util::FileEdit.new("nonexistfile")}.should raise_error
  41. end
  42. it "should throw an exception if the input file is blank" do
  43. lambda do
  44. Chef::Util::FileEdit.new(File.join(CHEF_SPEC_DATA, "filedit", "blank"))
  45. end.should raise_error
  46. end
  47. end
  48. describe "search_file_replace" do
  49. it "should accept regex passed in as a string (not Regexp object) and replace the match if there is one" do
  50. @fedit.search_file_replace("localhost", "replacement")
  51. @fedit.write_file
  52. newfile = File.new(@tempfile.path).readlines
  53. newfile[0].should match(/replacement/)
  54. end
  55. it "should accept regex passed in as a Regexp object and replace the match if there is one" do
  56. @fedit.search_file_replace(/localhost/, "replacement")
  57. @fedit.write_file
  58. newfile = File.new(@tempfile.path).readlines
  59. newfile[0].should match(/replacement/)
  60. end
  61. it "should do nothing if there isn't a match" do
  62. @fedit.search_file_replace(/pattern/, "replacement")
  63. @fedit.write_file
  64. newfile = File.new(@tempfile.path).readlines
  65. newfile[0].should_not match(/replacement/)
  66. end
  67. end
  68. describe "search_file_replace_line" do
  69. it "should search for match and replace the whole line" do
  70. @fedit.search_file_replace_line(/localhost/, "replacement line")
  71. @fedit.write_file
  72. newfile = File.new(@tempfile.path).readlines
  73. newfile[0].should match(/replacement/)
  74. newfile[0].should_not match(/127/)
  75. end
  76. end
  77. describe "search_file_delete" do
  78. it "should search for match and delete the match" do
  79. @fedit.search_file_delete(/localhost/)
  80. @fedit.write_file
  81. newfile = File.new(@tempfile.path).readlines
  82. newfile[0].should_not match(/localhost/)
  83. newfile[0].should match(/127/)
  84. end
  85. end
  86. describe "search_file_delete_line" do
  87. it "should search for match and delete the matching line" do
  88. @fedit.search_file_delete_line(/localhost/)
  89. @fedit.write_file
  90. newfile = File.new(@tempfile.path).readlines
  91. newfile[0].should_not match(/localhost/)
  92. newfile[0].should match(/broadcasthost/)
  93. end
  94. end
  95. describe "insert_line_after_match" do
  96. it "should search for match and insert the given line after the matching line" do
  97. @fedit.insert_line_after_match(/localhost/, "new line inserted")
  98. @fedit.write_file
  99. newfile = File.new(@tempfile.path).readlines
  100. newfile[1].should match(/new/)
  101. end
  102. end
  103. describe "insert_line_if_no_match" do
  104. it "should search for match and insert the given line if no line match" do
  105. @fedit.insert_line_if_no_match(/pattern/, "new line inserted")
  106. @fedit.write_file
  107. newfile = File.new(@tempfile.path).readlines
  108. newfile.last.should match(/new/)
  109. end
  110. it "should do nothing if there is a match" do
  111. @fedit.insert_line_if_no_match(/localhost/, "replacement")
  112. @fedit.write_file
  113. newfile = File.new(@tempfile.path).readlines
  114. newfile[1].should_not match(/replacement/)
  115. end
  116. end
  117. end