spec/scripts/script_spec.rb in config_scripts-0.4.1 vs spec/scripts/script_spec.rb in config_scripts-0.4.2
- old
+ new
@@ -35,137 +35,133 @@
end
describe "run_pending_scripts" do
class TestConfigScriptConfig < ConfigScripts::Scripts::Script; end
- let!(:timestamp1) { '20140208150000' }
- let!(:timestamp2) { '20140208200000' }
- let!(:timestamp3) { '20140208250000' }
+ let(:script_filenames) { [double, double, double] }
- let(:script_filenames) { [timestamp1, timestamp2, timestamp3].collect { |stamp| "#{stamp}_test_config_script" } }
-
- let!(:script1) { TestConfigScriptConfig.new(timestamp1) }
- let!(:script2) { TestConfigScriptConfig.new(timestamp2) }
- let!(:script3) { TestConfigScriptConfig.new(timestamp3) }
-
before do
klass.stub pending_scripts: script_filenames
- klass.stub :require
- klass.stub :puts
- [script1, script2, script3].each do |script|
- script.stub(:run)
+ klass.stub :run_file
+ end
+
+ context "with no problems running the scripts" do
+ before do
+ klass.run_pending_scripts
end
- TestConfigScriptConfig.stub(:new).with(timestamp1).and_return(script1)
- TestConfigScriptConfig.stub(:new).with(timestamp2).and_return(script2)
- TestConfigScriptConfig.stub(:new).with(timestamp3).and_return(script3)
+ it "runs the scripts" do
+ expect(klass).to have_received(:run_file).with(script_filenames[0])
+ expect(klass).to have_received(:run_file).with(script_filenames[1])
+ expect(klass).to have_received(:run_file).with(script_filenames[2])
+ end
+ end
- FileUtils.mkdir_p(Rails.root.join("tmp", "cache"))
+ context "with a problems running the scripts" do
+ before do
+ klass.stub(:run_file).with(script_filenames[1]).and_raise
+ end
+
+ it "raises an exception" do
+ expect(lambda{klass.run_pending_scripts}).to raise_exception
+ end
+
+ it "stops running the scripts when it hits the exception" do
+ klass.run_pending_scripts rescue nil
+ expect(klass).to have_received(:run_file).with(script_filenames[0])
+ expect(klass).to have_received(:run_file).with(script_filenames[1])
+ expect(klass).not_to have_received(:run_file).with(script_filenames[2])
+ end
end
+ end
- let(:scripts) {[
- {
- filename: script_filenames[0],
- script: script1,
- run: true
- },
- {
- filename: script_filenames[1],
- script: script2,
- run: true
- },
- {
- filename: script_filenames[2],
- script: script3,
- run: true
- }
- ]}
+ describe "run_file" do
+ class TestConfigScriptConfig < ConfigScripts::Scripts::Script; end
+ let(:timestamp) { '20140208150000' }
+ let(:filename) { "#{timestamp}_test_config_script" }
+ let!(:script) { TestConfigScriptConfig.new(timestamp) }
+ let(:path) { Rails.root.join("db", "config_scripts", "#{filename}.rb") }
- shared_examples "ran scripts" do
- it "requires only the scripts it needs to run" do
- scripts.each do |script|
- path = Rails.root.join("db", "config_scripts", "#{script[:filename]}.rb")
- if script[:run]
- expect(klass).to have_received(:require).with(path)
- else
- expect(klass).not_to have_received(:require).with(path)
- end
- end
+ before do
+ script.stub :run
+ TestConfigScriptConfig.stub new: script
+ klass.stub :require
+ klass.stub :puts
+ end
+
+ context "with no problems running the scripts" do
+ before do
+ klass.run_file(filename, :sideways)
end
- it "creates a config script for each timestamp with the appropriate class" do
- scripts.each do |script|
- timestamp = script[:filename].first(14)
- if script[:run]
- expect(TestConfigScriptConfig).to have_received(:new).with(timestamp)
- else
- expect(TestConfigScriptConfig).not_to have_received(:new).with(timestamp)
- end
- end
+ it "requires the file" do
+ expect(klass).to have_received(:require).with(path)
end
- it "calls the up command on each script item" do
- scripts.each do |script|
- if script[:run]
- expect(script[:script]).to have_received(:run).with(:up)
- else
- expect(script[:script]).not_to have_received(:run)
- end
- end
+ it "puts the name of the file out to the console" do
+ expect(klass).to have_received(:puts).with("Running #{filename} sideways")
end
- it "outputs the name of the scripts that it is running" do
- scripts.each do |script|
- if script[:run]
- expect(klass).to have_received(:puts).with("Running #{script[:filename]}")
- else
- expect(klass).not_to have_received(:puts).with("Running #{script[:filename]}")
- end
- end
+ it "creates a config script with the class and timestamp" do
+ expect(TestConfigScriptConfig).to have_received(:new).with(timestamp)
end
+
+ it "calls the run command on the script with the direction" do
+ expect(script).to have_received(:run).with(:sideways)
+ end
end
- context "with no problems running the scripts" do
- it_behaves_like "ran scripts"
-
+ context "with a problem running the script" do
before do
- klass.run_pending_scripts
+ script.stub(:run).and_raise 'error'
end
+
+ it "re-raises the exception" do
+ expect(lambda{klass.run_file(filename)}).to raise_exception 'error'
+ end
end
- context "with a problems running the scripts" do
- it_behaves_like "ran scripts"
+ context "with a problem finding the class" do
+ it "re-raises the exception" do
+ expect(lambda{klass.run_file("#{timestamp}_test_config_script_2")}).to raise_exception NameError
+ end
+ end
+ end
+ describe "filename_for_script" do
+ let(:name) { 'my_change' }
+ let(:path) { File.join(klass.script_directory, "*#{name}.rb") }
+ let(:filename) { "1234512345_#{name}" }
+ subject { klass.filename_for_script(name) }
+
+ context "when there are multiple files that have the name" do
before do
- script2.stub(:run).and_raise 'Error in script'
- scripts[2][:run] = false
- begin
- klass.run_pending_scripts
- rescue
- end
+ Dir.stub(:glob).with(path).and_return(["#{filename}.rb", "5123451234_#{name}.rb"])
end
+
+ it "is the first one" do
+ expect(subject).to eq filename
+ end
end
- context "with a class name it cannot find" do
- let(:bad_filename) { "20140208170000_missing_class" }
-
+ context "when there is one file with the exact name, and one that just ends with the name" do
before do
- scripts[1][:run] = false
- scripts[2][:run] = false
- klass.stub pending_scripts: [script_filenames[0], bad_filename, script_filenames[1], script_filenames[2]]
- klass.run_pending_scripts
+ Dir.stub(:glob).with(path).and_return(["5123451234_prepare_#{name}.rb", "#{filename}.rb"])
end
- it_behaves_like "ran scripts"
+ it "is the one with the exact name" do
+ expect(subject).to eq filename
+ end
+ end
- it "requires the path for the bad script" do
- path = Rails.root.join("db", "config_scripts", "#{bad_filename}.rb")
- expect(klass).to have_received(:require).with(path)
+ context "when there are no files with the name" do
+ before do
+ Dir.stub(:glob).with(path).and_return([])
end
- it "outputs a name error for the missing class" do
- expect(klass).to have_received(:puts).with("Aborting: could not find class MissingClassConfig")
+ it "is nil" do
+ expect(subject).to be_nil
end
end
end
describe "list_pending_scripts" do
\ No newline at end of file