Skip to content

Commit

Permalink
Merge branch 'ruby-github-workflow'
Browse files Browse the repository at this point in the history
  • Loading branch information
gbp committed Jul 28, 2023
2 parents 57a3dce + 6081477 commit 1dac609
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 12 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Ruby Tests and Specs

on:
push:
branches: master
pull_request:

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 3.2
bundler-cache: true
working-directory: ./rblib

- name: Run tests
working-directory: ./rblib
run: bundle exec rake test:commonlib

- name: Run specs
working-directory: ./rblib
run: bundle exec rake spec:commonlib
11 changes: 11 additions & 0 deletions rblib/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
source 'https://rubygems.org'

gem 'rake'

gem 'rspec'
gem 'test-unit'

gem 'mocha'
gem 'open4'
gem 'unidecoder'
gem 'webmock'
53 changes: 53 additions & 0 deletions rblib/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
GEM
remote: https://rubygems.org/
specs:
addressable (2.8.4)
public_suffix (>= 2.0.2, < 6.0)
crack (0.4.5)
rexml
diff-lcs (1.5.0)
hashdiff (1.0.1)
mocha (2.0.4)
ruby2_keywords (>= 0.0.5)
open4 (1.3.4)
power_assert (2.0.3)
public_suffix (5.0.3)
rake (13.0.6)
rexml (3.2.6)
rspec (3.12.0)
rspec-core (~> 3.12.0)
rspec-expectations (~> 3.12.0)
rspec-mocks (~> 3.12.0)
rspec-core (3.12.2)
rspec-support (~> 3.12.0)
rspec-expectations (3.12.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-mocks (3.12.6)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-support (3.12.1)
ruby2_keywords (0.0.5)
test-unit (3.6.1)
power_assert
unidecoder (1.1.2)
webmock (3.18.1)
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)

PLATFORMS
arm64-darwin-22
x86_64-linux

DEPENDENCIES
mocha
open4
rake
rspec
test-unit
unidecoder
webmock

BUNDLED WITH
2.4.10
2 changes: 1 addition & 1 deletion rblib/tests/external_command_scripts/cat.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/sh

while read line
do
Expand Down
4 changes: 2 additions & 2 deletions rblib/tests/external_command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
it "should be able to limit memory available to its children" do
f = ExternalCommand.new(malloc_script, :memory_limit => 1048576 * 128).run
f.out.should == ""
f.err.should == "Out of memory!\n"
f.err.should =~ /^Out of memory!/
end

it "should not limit the memory available to calling code" do
Expand Down Expand Up @@ -138,7 +138,7 @@
it 'should raise a ChildUnterminated exception if the process cannot be terminated' do
start_time = Time.now
external_command = ExternalCommand.new(sleep_cmd, "30", :timeout => 2)
external_command.stub!(:try_to_kill).and_return(false)
allow(external_command).to receive(:try_to_kill).and_return(false)
lambda { f = external_command.run }.should raise_error(ExternalCommand::ChildUnterminated)
(Time.now - start_time).should < 5
external_command.timed_out.should == true
Expand Down
3 changes: 1 addition & 2 deletions rblib/tests/spec.rake
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ namespace :spec do

desc "Run the ruby test in commonlib in rspec format"
RSpec::Core::RakeTask.new(:commonlib) do |t|
t.ruby_opts = ['-rtest/unit']
t.pattern = File.join(File.dirname(__FILE__), '*_{test,spec}.rb')
t.pattern = File.join(File.dirname(__FILE__), '*_spec.rb')
end

end
41 changes: 34 additions & 7 deletions rblib/tests/survey_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,70 @@
require 'config'
require 'survey'
require 'test/unit'

# We should really use a synthetic config file for testing, but the survey
# module requires a real running instance of the mySociety survey service
# and so can’t really be tested in splendid isolation. Therefore we make an
# educated guess at where we might find a config file that contains connection
# details for a running instance of the service.
MySociety::Config::set_file(File.join(File.dirname(__FILE__), "..", "..", "..", "config", "general"))
require 'mocha/test_unit'
require 'webmock/test_unit'

class TestSurvey < Test::Unit::TestCase

def setup
MySociety::Config.stubs(:get).
with('SURVEY_URL').returns('https://example.com/survey')

MySociety::Config.stubs(:get).
with('SURVEY_SECRET').returns('ABC123')

MySociety::Config.stubs(:get).
with('SSL_CA_PATH', '/etc/ssl/certs/').returns('/etc/ssl/certs/')

@survey = MySociety::Survey.new "survey_test.rb", "[email protected]"
end

def test_submit_ok
# Just check we can submit with no exceptions
return_url = "http://localhost/"

stub_request(:post, 'https://example.com/survey')
.with do |req|
data = URI.decode_www_form(req.body).to_h
data['foo'] == 'bar' && data['return_url'] == return_url
end
.to_return(status: 302, headers: { 'Location' => return_url })

assert_equal(return_url, @survey.submit("foo" => "bar", "return_url" => return_url))
end

def test_already_done_no
stub_request(:post, 'https://example.com/survey')
.to_return(status: 200, body: '0')

survey_we_never_do = MySociety::Survey.new "survey_test.rb", "[email protected]"
assert !survey_we_never_do.already_done?
end

def test_already_done_yes
stub_request(:post, 'https://example.com/survey')
.to_return(status: 200, body: '1')

@survey.submit("foo" => "bar", "return_url" => "")
assert @survey.already_done?
end

def test_allow_new_survey
stub_request(:post, 'https://example.com/survey')
.to_return(status: 200, body: '1')

@survey.submit("foo" => "bar", "return_url" => "")
assert @survey.already_done?

stub_request(:post, 'https://example.com/survey')
.to_return(status: 200, body: '0')

@survey.allow_new_survey
assert !@survey.already_done?

stub_request(:post, 'https://example.com/survey')
.to_return(status: 200, body: '1')

@survey.submit("foo" => "bar", "return_url" => "")
assert @survey.already_done?
end
Expand Down

0 comments on commit 1dac609

Please sign in to comment.