Skip to content

Commit

Permalink
Add automated tests for HTML generation
Browse files Browse the repository at this point in the history
  • Loading branch information
shepmaster committed May 1, 2024
1 parent eefa5c2 commit 4c9bd79
Show file tree
Hide file tree
Showing 12 changed files with 391 additions and 1 deletion.
1 change: 1 addition & 0 deletions integration-tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spec/examples.txt
1 change: 1 addition & 0 deletions integration-tests/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper
30 changes: 30 additions & 0 deletions integration-tests/.rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# https://docs.rubocop.org/rubocop/configuration

require:
- rubocop-capybara
- rubocop-rspec

AllCops:
NewCops: enable
TargetRubyVersion: 3.3

Style/TrailingCommaInArguments:
EnforcedStyleForMultiline: consistent_comma

Style/TrailingCommaInArrayLiteral:
EnforcedStyleForMultiline: consistent_comma

Style/TrailingCommaInHashLiteral:
EnforcedStyleForMultiline: consistent_comma

Metrics/AbcSize:
Enabled: false

Metrics/MethodLength:
Max: 50

RSpec/ExampleLength:
Max: 50

RSpecRails/InferredSpecType:
Enabled: false
12 changes: 12 additions & 0 deletions integration-tests/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

source 'https://rubygems.org'

gem 'capybara', '~> 3.40'
gem 'rspec', '~> 3.13'
gem 'selenium-webdriver', '~> 4.20'
gem 'webrick', '~> 1.8'

gem 'rubocop', '~> 1.63', require: false
gem 'rubocop-capybara', '~> 2.20', require: false
gem 'rubocop-rspec', '~> 2.29', require: false
114 changes: 114 additions & 0 deletions integration-tests/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
GEM
remote: https://rubygems.org/
specs:
addressable (2.8.6)
public_suffix (>= 2.0.2, < 6.0)
ast (2.4.2)
base64 (0.2.0)
capybara (3.40.0)
addressable
matrix
mini_mime (>= 0.1.3)
nokogiri (~> 1.11)
rack (>= 1.6.0)
rack-test (>= 0.6.3)
regexp_parser (>= 1.5, < 3.0)
xpath (~> 3.2)
diff-lcs (1.5.1)
json (2.7.2)
language_server-protocol (3.17.0.3)
matrix (0.4.2)
mini_mime (1.1.5)
nokogiri (1.16.4-aarch64-linux)
racc (~> 1.4)
nokogiri (1.16.4-arm-linux)
racc (~> 1.4)
nokogiri (1.16.4-arm64-darwin)
racc (~> 1.4)
nokogiri (1.16.4-x86-linux)
racc (~> 1.4)
nokogiri (1.16.4-x86_64-darwin)
racc (~> 1.4)
nokogiri (1.16.4-x86_64-linux)
racc (~> 1.4)
parallel (1.24.0)
parser (3.3.1.0)
ast (~> 2.4.1)
racc
public_suffix (5.0.5)
racc (1.7.3)
rack (3.0.10)
rack-test (2.1.0)
rack (>= 1.3)
rainbow (3.1.1)
regexp_parser (2.9.0)
rexml (3.2.6)
rspec (3.13.0)
rspec-core (~> 3.13.0)
rspec-expectations (~> 3.13.0)
rspec-mocks (~> 3.13.0)
rspec-core (3.13.0)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-mocks (3.13.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-support (3.13.1)
rubocop (1.63.4)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
parser (>= 3.3.0.2)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.31.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.31.3)
parser (>= 3.3.1.0)
rubocop-capybara (2.20.0)
rubocop (~> 1.41)
rubocop-factory_bot (2.25.1)
rubocop (~> 1.41)
rubocop-rspec (2.29.1)
rubocop (~> 1.40)
rubocop-capybara (~> 2.17)
rubocop-factory_bot (~> 2.22)
rubocop-rspec_rails (~> 2.28)
rubocop-rspec_rails (2.28.3)
rubocop (~> 1.40)
ruby-progressbar (1.13.0)
rubyzip (2.3.2)
selenium-webdriver (4.20.1)
base64 (~> 0.2)
rexml (~> 3.2, >= 3.2.5)
rubyzip (>= 1.2.2, < 3.0)
websocket (~> 1.0)
unicode-display_width (2.5.0)
webrick (1.8.1)
websocket (1.2.10)
xpath (3.2.0)
nokogiri (~> 1.8)

PLATFORMS
aarch64-linux
arm-linux
arm64-darwin
x86-linux
x86_64-darwin
x86_64-linux

DEPENDENCIES
capybara (~> 3.40)
rspec (~> 3.13)
rubocop (~> 1.63)
rubocop-capybara (~> 2.20)
rubocop-rspec (~> 2.29)
selenium-webdriver (~> 4.20)
webrick (~> 1.8)

BUNDLED WITH
2.5.9
41 changes: 41 additions & 0 deletions integration-tests/spec/features/crate_list_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

require 'scratch_space'

RSpec.describe 'The crate list', type: :feature do
let(:scratch) { ScratchSpace.new }
let(:registry) { scratch.registry }

before { registry.start }

after do
registry.stop
scratch.cleanup
end

it 'shows the crate and version' do
names = %w[a bb ccc dddd]

names.each do |name|
scratch
.crate(name:, version: '1.0.0')
.lib_rs(%(pub const ID: &str = "#{name}";))
.publish_to(registry)
end

visit registry.url

within(:section, 'Available crates') do
names.each do |name|
aggregate_failures do
expect(page).to have_content(name)
expect(page).to have_select('version', with_options: ['1.0.0'])
end
end
end
end

Capybara.add_selector(:section) do
xpath { |title| ".//section[h1[contains(., '#{title}')]]" }
end
end
35 changes: 35 additions & 0 deletions integration-tests/spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

require 'capybara/rspec'

$LOAD_PATH << 'spec/support'

RSpec.configure do |config|
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end

config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end

config.shared_context_metadata_behavior = :apply_to_host_groups

config.filter_run_when_matching :focus

config.example_status_persistence_file_path = 'spec/examples.txt'

config.disable_monkey_patching!

config.warnings = true

config.default_formatter = 'doc' if config.files_to_run.one?

config.profile_examples = 10

config.order = :random
Kernel.srand config.seed
end

Capybara.default_driver = Capybara.javascript_driver = :selenium_headless
Capybara.run_server = false
53 changes: 53 additions & 0 deletions integration-tests/spec/support/crate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# frozen_string_literal: true

class Crate
attr_reader :name, :version

def initialize(name:, version:, root:)
@name = name
@version = version
@root = root
@source = {}
end

def lib_rs(content)
@source['lib.rs'] = content
self
end

def publish_to(registry)
Dir.mkdir(@root)

cargo_toml = @root.join('Cargo.toml')
File.open(cargo_toml, 'w') do |f|
content = <<~TOML
[package]
name = "#{@name}"
version = "#{@version}"
edition = "2021"
TOML
f.write(content)
end

src = @root.join('src')
Dir.mkdir(src)

@source.each do |name, content|
file = src.join(name)
File.write(file, content)
end

system('cargo', 'package', '--quiet', chdir: @root, exception: true)
package = @root.join('target', 'package', "#{name}-#{version}.crate")

system(
'../target/debug/margo',
'add',
'--registry',
registry.root.to_s,
package.to_s,
%i[out err] => File::NULL,
exception: true,
)
end
end
38 changes: 38 additions & 0 deletions integration-tests/spec/support/registry.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

require 'registry_server'

class Registry
attr_reader :root

def initialize(root)
@root = root
@server = RegistryServer.new(@root)
end

def start
@server.start
system(
'../target/debug/margo',
'init',
'--base-url',
url,
# TODO: Add a `--defaults`
'--html',
'true',
'--html-suggested-registry-name',
'qwidge',
@root.to_s,
%i[out err] => File::NULL,
exception: true,
)
end

def stop
@server.stop
end

def url
@server.url
end
end
38 changes: 38 additions & 0 deletions integration-tests/spec/support/registry_server.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

require 'webrick'

class RegistryServer
def initialize(root)
@server = WEBrick::HTTPServer.new(
DocumentRoot: root,
BindAddress: '127.0.0.1',
Port: 0,
Logger: WEBrick::Log.new(IO::NULL),
AccessLog: [],
)
end

def start
@thread = Thread.new do
@server.start
end
end

def stop
@server.shutdown
@thread.join
end

def url
"http://#{address}:#{port}/"
end

def address
@server.config[:BindAddress]
end

def port
@server.config[:Port]
end
end
Loading

0 comments on commit 4c9bd79

Please sign in to comment.