/spec/build/job/test_spec.rb

https://github.com/loicfrering/travis-build · Ruby · 207 lines · 168 code · 38 blank · 1 comment · 18 complexity · 11d55e888de67fe53eb0f46b6ed80d2f MD5 · raw file

  1. require 'spec_helper'
  2. require 'travis/build'
  3. require 'hashr'
  4. describe Travis::Build::Job::Test do
  5. let(:shell) { stub('shell', :chdir => true, :export_line => true, :execute => true, :cwd => '~/builds', :file_exists? => true, :echo => nil) }
  6. let(:commit) { stub(:checkout => true) }
  7. let(:config) { Hashr.new(:env => 'FOO=foo', :script => 'rake') }
  8. let(:job) { Travis::Build::Job::Test.new(shell, commit, config) }
  9. describe 'by_lang' do
  10. Test = Travis::Build::Job::Test
  11. it 'returns Test::Ruby for nil' do
  12. Test.by_lang(nil).should == Test::Ruby
  13. end
  14. it 'returns Test::Ruby for an unknown language' do
  15. Test.by_lang('brainfuck').should == Test::Ruby
  16. end
  17. it 'returns Test::Ruby for "ruby"' do
  18. Test.by_lang('ruby').should == Test::Ruby
  19. Test.by_lang('Ruby').should == Test::Ruby
  20. end
  21. it 'returns Test::Clojure for "clojure"' do
  22. Test.by_lang('clojure').should == Test::Clojure
  23. Test.by_lang('Clojure').should == Test::Clojure
  24. end
  25. it 'returns Test::Erlang for "erlang"' do
  26. Test.by_lang('erlang').should == Test::Erlang
  27. Test.by_lang('Erlang').should == Test::Erlang
  28. end
  29. # JRuby won't let us use a class named Java. MK.
  30. it 'returns Test::PureJava for "java"' do
  31. Test.by_lang('java').should == Test::PureJava
  32. Test.by_lang('JAVA').should == Test::PureJava
  33. end
  34. it 'returns Test::NodeJs for "node_js"' do
  35. Test.by_lang('node_js').should == Test::NodeJs
  36. end
  37. it 'returns Test::Php for "php"' do
  38. Test.by_lang('php').should == Test::Php
  39. Test.by_lang('PHP').should == Test::Php
  40. end
  41. it 'returns Test::Scala for "scala"' do
  42. Test.by_lang('scala').should == Test::Scala
  43. Test.by_lang('Scala').should == Test::Scala
  44. end
  45. end
  46. describe 'run' do
  47. it 'changes to the build dir' do
  48. shell.expects(:chdir).with('~/builds')
  49. job.run
  50. end
  51. it 'checks the given commit out' do
  52. commit.expects(:checkout).returns(true)
  53. job.run
  54. end
  55. it 'sets the project up' do
  56. shell.expects(:export_line).with('FOO=foo')
  57. job.run
  58. end
  59. it 'installs dependencies' do
  60. job.expects(:install)
  61. job.run
  62. end
  63. it 'runs the scripts from the configuration' do
  64. job.expects(:run_commands)
  65. job.run
  66. end
  67. it 'returns { :result => 0 } if the last script returned true' do
  68. shell.expects(:execute).with('rake', :stage => :script).returns(true)
  69. job.run.should == { :result => 0 }
  70. end
  71. it 'returns { :result => 1 } if the last script returned false' do
  72. shell.expects(:execute).with('rake', :stage => :script).returns(false)
  73. job.run.should == { :result => 1 }
  74. end
  75. it 'returns { :result => 1 } if checkout raised an exception' do
  76. commit.expects(:checkout).returns(false)
  77. job.run.should == { :result => 1 }
  78. end
  79. end
  80. describe 'export' do
  81. it 'accepts a single string with multiple values' do
  82. s = 'SUITE=integration'
  83. config.env = s
  84. shell.expects(:export_line).with(s)
  85. job.send(:export)
  86. end
  87. it 'accepts a single string with multiple values' do
  88. s = 'FOO=foo BAR=2 BAZ="test values/baz"'
  89. config.env = s
  90. shell.expects(:export_line).with(s)
  91. job.send(:export)
  92. end
  93. it 'accepts an array of strings' do
  94. s1 = 'FOO=foo'
  95. s2 = 'BAR=bar BAZ="test test/ci"'
  96. config.env = [s1, s2]
  97. shell.expects(:export_line).with(s1)
  98. shell.expects(:export_line).with(s2)
  99. job.send(:export)
  100. end
  101. end
  102. describe 'run_stages' do
  103. it 'runs all the command stages in order if they return true' do
  104. [:before_install, :install, :before_script, :script, :after_script].each do |stage|
  105. job.expects(:run_commands).with(stage).returns(true).once
  106. end
  107. job.send(:run_stages)
  108. end
  109. it 'does not run all the command stages if one returns false' do
  110. job.expects(:run_commands).with(:before_install).returns(true).once
  111. job.expects(:run_commands).with(:install).returns(false).once
  112. job.expects(:run_commands).with(:before_script).never
  113. job.send(:run_stages)
  114. end
  115. end
  116. describe 'run_commands' do
  117. before :each do
  118. job.config.clear
  119. end
  120. [:before_script, :script, :after_script].each do |stage|
  121. it "does not run any #{stage}s if the config does not define them" do
  122. job.expects(:run_command).never
  123. job.send(:run_commands, stage)
  124. end
  125. it "runs a single #{stage} defined in the config" do
  126. job.config[stage] = './foo'
  127. job.expects(:run_command).with(stage, './foo').returns(true)
  128. job.send(:run_commands, stage)
  129. end
  130. it "runs an array of #{stage}s defined in the config" do
  131. job.config[stage] =['./foo', './bar']
  132. job.expects(:run_command).with(stage, './foo').returns(true)
  133. job.expects(:run_command).with(stage, './bar').returns(true)
  134. job.send(:run_commands, stage)
  135. end
  136. end
  137. [:before_install, :install, :before_script, :script, :after_script].each do |stage|
  138. it "runs #{stage} as defined in the config" do
  139. job.config[stage] = "./#{stage}"
  140. job.expects(:run_command).with(stage,"./#{stage}").returns(true)
  141. job.send(:run_commands, stage)
  142. end
  143. end
  144. it 'does not run the second before_script if the first one fails' do
  145. job.config.before_script = ['./before', './before_another']
  146. job.expects(:run_command).with(:before_script, './before').returns(false)
  147. job.expects(:run_command).with(:before_script, './before_another').never
  148. job.send(:run_commands, :before_script)
  149. end
  150. end
  151. describe 'run_command' do
  152. it 'returns true if the given script yields true' do
  153. shell.expects(:execute).returns(true)
  154. job.send(:run_command, :script, './foo').should be_true
  155. end
  156. it 'returns false if the given script yields false' do
  157. shell.expects(:execute).returns(false)
  158. job.send(:run_command, :script, './foo').should be_false
  159. end
  160. context "when a before_script has failed" do
  161. it 'echos a message to the shell' do
  162. job.config.before_script = './before'
  163. shell.expects(:execute).with('./before', { :stage => :before_script }).returns(false)
  164. shell.expects(:echo).with("\n\nbefore_script: './before' returned false.")
  165. job.send(:run_command, :before_script, './before')
  166. end
  167. end
  168. end
  169. end