forked from solidusio/solidus_dev_support
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_extension_spec.rb
More file actions
192 lines (163 loc) · 5.61 KB
/
create_extension_spec.rb
File metadata and controls
192 lines (163 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# frozen_string_literal: true
require "fileutils"
require "open3"
require "spec_helper"
require "spree/core/version"
RSpec.describe "Create extension" do
include FileUtils
let(:gem_root) { File.expand_path("../..", __dir__) }
let(:solidus_cmd) { "#{gem_root}/exe/solidus" }
let(:extension_name) { "test_extension" }
let(:gemspec_name) { "solidus_#{extension_name}.gemspec" }
let(:tmp_path) { Pathname.new(gem_root).join("tmp") }
let(:install_path) { tmp_path.join("solidus_#{extension_name}") }
let(:command_failed_error) { Class.new(StandardError) }
before do
rm_rf(install_path)
mkdir_p(tmp_path)
end
def step(method_name)
puts "Step #{method_name}"
send method_name
end
it "checks the create extension process" do
step :check_solidus_cmd
step :check_gem_version
step :check_create_extension
step :check_bundle_install
step :check_default_task
step :check_run_specs
step :check_sandbox
end
private
def check_solidus_cmd
cd(tmp_path) do
output = `#{solidus_cmd} -h`
expect($?).to be_success
expect(output).to include("Commands:")
end
end
def check_gem_version
gem_version_commands = ["version", "--version", "-v"]
gem_version = SolidusDevSupport::VERSION
solidus_version = Spree.solidus_gem_version
cd(tmp_path) do
gem_version_commands.each do |gem_version_cmd|
output = `#{solidus_cmd} #{gem_version_cmd}`
expect($?).to be_success
expect(output).to include("Solidus version #{solidus_version}")
expect(output).to include("Solidus Dev Support version #{gem_version}")
end
end
end
def check_create_extension
cd(tmp_path) do
output = `#{solidus_cmd} extension #{extension_name}`
expect($?).to be_success
expect(output).to include(gemspec_name)
expect(output).to include(".circleci")
end
cd(install_path) do
aggregate_failures do
%w[
bin/console
bin/rails
bin/rails-engine
bin/rails-sandbox
bin/sandbox
bin/setup
].each do |bin|
bin = Pathname(bin)
expect(
name: bin,
exist: bin.exist?,
executable: bin.stat.executable?
).to eq(
name: bin,
exist: true,
executable: true
)
end
end
end
end
def check_bundle_install
cd(install_path) do
open("Gemfile", "a") { |f| f.puts "gem 'solidus_dev_support', path: '../..'" }
end
# Update gemspec with the required fields
gemspec_path = install_path.join(gemspec_name)
gemspec = gemspec_path.read.lines
gemspec.grep(/spec\.author/).first.replace(" spec.author = 'John Doe'\n")
gemspec.grep(/spec\.email/).first.replace(" spec.email = 'john@example.com'\n")
gemspec.grep(/spec\.summary/).first.replace(" spec.summary = 'A nice extension'\n")
gemspec.grep(/spec\.description/).first.replace(" spec.description = 'A super nice extension'\n")
gemspec_path.write(gemspec.join)
expect(bundle_install).to match(/Bundle complete/)
end
def check_default_task
cd(install_path) do
output = sh("bin/rake")
expect(output).to include("0 examples, 0 failures")
end
end
def check_run_specs
install_path.join("lib", "solidus_test_extension", "testing_support", "factories.rb").open("a") do |factories_file|
factories_file.write "\n puts 'loading test_extension factories'"
end
install_path.join("spec", "some_spec.rb").write(
"require 'spec_helper'\nRSpec.describe 'Some test' do it { expect(true).to be_truthy } end\n"
)
cd(install_path) do
output = sh("bundle exec rspec")
expect(output).to include("loading test_extension factories")
expect(output).to include("1 example, 0 failures")
expect(output).to include(ENV["CODECOV_TOKEN"] ? "Coverage reports upload successfully" : "Provide a CODECOV_COVERAGE_PATH environment variable to enable Codecov uploads")
end
end
def check_sandbox
cd(install_path) do
command = 'bin/rails-sandbox runner "puts %{The version of SolidusTestExtension is #{SolidusTestExtension::VERSION}}"'
first_run_output = sh(command)
expect(first_run_output).to include("Creating the sandbox app...")
expect(first_run_output).to include("The version of SolidusTestExtension is 0.0.1")
second_run_output = sh(command)
expect(second_run_output).not_to include("Creating the sandbox app...")
expect(second_run_output).to include("The version of SolidusTestExtension is 0.0.1")
end
end
def sh(*args)
command = (args.size == 1) ? args.first : args.shelljoin
output, status = with_unbundled_env do
Open3.capture2e({"CI" => nil}, command)
end
if $DEBUG || ENV["DEBUG"]
warn "~" * 80
warn "$ #{command}"
warn output
warn "$ #{command} ~~~~> EXIT STATUS: #{status.exitstatus}"
end
unless status.success?
raise(command_failed_error, "command failed: #{command}\n#{output}")
end
output.to_s
end
def with_unbundled_env(...)
if Bundler.respond_to?(:with_unbundled_env)
Bundler.with_unbundled_env(...)
else
Bundler.with_clean_env(...)
end
end
def bundle_install
# Optimize the bundle path within the CI, in this context using bundler env
# variables doesn't help because commands are run with a clean env.
bundle_path = "#{gem_root}/vendor/bundle"
if File.exist?(bundle_path)
sh "bundle config set --local path #{bundle_path.shellescape}"
end
output = nil
cd(install_path) { output = sh "bundle install" }
output
end
end