Skip to content

Commit

Permalink
Expose packs/rspec/support for easier ecosystem testing (#8)
Browse files Browse the repository at this point in the history
* add require for pathname

* add note

* bump version
  • Loading branch information
Alex Evanczuk authored Dec 29, 2022
1 parent 6f92e09 commit 796976c
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 41 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
packs (0.0.4)
packs (0.0.5)
sorbet-runtime

GEM
Expand Down
1 change: 1 addition & 0 deletions lib/packs.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# typed: strict

require 'yaml'
require 'pathname'
require 'sorbet-runtime'
require 'packs/pack'
require 'packs/private'
Expand Down
33 changes: 33 additions & 0 deletions lib/packs/rspec/fixture_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# typed: strict
# frozen_string_literal: true

module FixtureHelper
extend T::Sig

sig { params(path: String, content: String).returns(String) }
def write_file(path, content = '')
pathname = Pathname.pwd.join(path)
FileUtils.mkdir_p(pathname.dirname)
pathname.write(content)
path
end

sig do
params(
pack_name: String,
config: T::Hash[T.untyped, T.untyped]
).void
end
def write_pack(
pack_name,
config = {}
)
path = Pathname.pwd.join(pack_name).join('package.yml')
write_file(path.to_s, YAML.dump(config))
end

sig { params(path: String).void }
def delete_app_file(path)
File.delete(path)
end
end
21 changes: 21 additions & 0 deletions lib/packs/rspec/support.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require_relative 'fixture_helper'

RSpec.configure do |config|
config.include FixtureHelper

config.before do
# We bust_cache always because each test may write its own packs
Packs.bust_cache!
end

# Eventually, we could make this opt-in via metadata so someone can use this support without affecting all their tests.
config.around do |example|
prefix = [File.basename($0), Process.pid].join('-') # rubocop:disable Style/SpecialGlobalVars
tmpdir = Dir.mktmpdir(prefix)
Dir.chdir(tmpdir) do
example.run
end
ensure
FileUtils.rm_rf(tmpdir)
end
end
2 changes: 1 addition & 1 deletion packs.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |spec|
spec.name = 'packs'
spec.version = '0.0.4'
spec.version = '0.0.5'
spec.authors = ['Gusto Engineers']
spec.email = ['[email protected]']
spec.summary = 'Packs are the specification for gradual modularization in the `rubyatscale` ecosystem.'
Expand Down
38 changes: 19 additions & 19 deletions spec/packs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@
describe '.all' do
context 'in app with a simple package' do
before do
write_file('packs/my_pack/package.yml')
write_pack('packs/my_pack')
end

it { expect(Packs.all.count).to eq 1 }
end

context 'in an app with nested packs' do
before do
write_file('packs/my_pack/package.yml')
write_file('packs/my_pack/subpack/package.yml')
write_pack('packs/my_pack')
write_pack('packs/my_pack/subpack')
end

it { expect(Packs.all.count).to eq 2 }
end

context 'in an app with a differently configured root' do
before do
write_file('packs/my_pack/package.yml')
write_file('components/my_pack/package.yml')
write_pack('packs/my_pack')
write_pack('components/my_pack')
write_file('packs.yml', <<~YML)
pack_paths:
- packs/*
Expand All @@ -35,8 +35,8 @@

context 'in an app with a differently configured root configured via ruby' do
before do
write_file('packs/my_pack/package.yml')
write_file('components/my_pack/package.yml')
write_pack('packs/my_pack')
write_pack('components/my_pack')
Packs.configure do |config|
config.pack_paths = ['packs/*', 'components/*']
end
Expand All @@ -47,9 +47,9 @@

context 'in an app with a differently configured root configured via ruby and YML' do
before do
write_file('packs/my_pack/package.yml')
write_file('components/my_pack/package.yml')
write_file('packages/my_pack/package.yml')
write_pack('packs/my_pack')
write_pack('components/my_pack')
write_pack('packages/my_pack')
write_file('packs.yml', <<~YML)
pack_paths:
- packs/*
Expand All @@ -69,16 +69,16 @@
describe '.find' do
context 'in app with a simple package' do
before do
write_file('packs/my_pack/package.yml')
write_pack('packs/my_pack')
end

it { expect(Packs.find('packs/my_pack').name).to eq 'packs/my_pack' }
end

context 'in an app with nested packs' do
before do
write_file('packs/my_pack/package.yml')
write_file('packs/my_pack/subpack/package.yml')
write_pack('packs/my_pack')
write_pack('packs/my_pack/subpack')
end

it { expect(Packs.find('packs/my_pack').name).to eq 'packs/my_pack' }
Expand All @@ -87,8 +87,8 @@

context 'in an app with a differently configured root' do
before do
write_file('packs/my_pack/package.yml')
write_file('components/my_pack/package.yml')
write_pack('packs/my_pack')
write_pack('components/my_pack')
write_file('packs.yml', <<~YML)
pack_paths:
- packs/*
Expand All @@ -103,8 +103,8 @@

describe '.for_file' do
before do
write_file('packs/package_1/package.yml')
write_file('packs/package_1_new/package.yml')
write_pack('packs/package_1')
write_pack('packs/package_1_new')
end

context 'given a filepath in pack_1' do
Expand All @@ -129,9 +129,9 @@

context 'in an app with nested packs' do
before do
write_file('packs/my_pack/package.yml')
write_pack('packs/my_pack')
write_file('packs/my_pack/file.rb')
write_file('packs/my_pack/subpack/package.yml')
write_pack('packs/my_pack/subpack')
write_file('packs/my_pack/subpack/file.rb')
end

Expand Down
22 changes: 2 additions & 20 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
require 'packs'
require 'pry'

require 'packs/rspec/support'

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = '.rspec_status'
Expand All @@ -14,24 +16,4 @@
config.expect_with :rspec do |c|
c.syntax = :expect
end

config.before do
Packs.bust_cache!
end

config.around do |example|
prefix = [File.basename($0), Process.pid].join('-') # rubocop:disable Style/SpecialGlobalVars
tmpdir = Dir.mktmpdir(prefix)
Dir.chdir(tmpdir) do
example.run
end
ensure
FileUtils.rm_rf(tmpdir)
end
end

def write_file(path, content = '')
pathname = Pathname.pwd.join(path)
FileUtils.mkdir_p(pathname.dirname)
pathname.write(content)
end

0 comments on commit 796976c

Please sign in to comment.