-
-
Notifications
You must be signed in to change notification settings - Fork 392
/
generators_spec.rb
154 lines (122 loc) · 5.08 KB
/
generators_spec.rb
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
# frozen_string_literal: true
if ENV["SIMPLECOV"]
require "simplecov"
SimpleCov.add_filter "/lib/decidim/generators/app_templates/"
SimpleCov.add_filter "/lib/decidim/generators/component_templates/"
end
require "spec_helper"
require "decidim/gem_manager"
module Decidim
describe Generators do
let(:env) do |example|
#
# When tracking coverage, make sure the ruby environment points to the
# local version, so we get the benefits of running `decidim` directly
# without `bundler` (more realistic test), but also get code coverage
# properly measured (we track coverage on the local version and not on the
# installed version).
#
if ENV["SIMPLECOV"]
{
"RUBYOPT" => "-rsimplecov #{ENV["RUBYOPT"]}",
"RUBYLIB" => "#{repo_root}/decidim-generators/lib:#{ENV["RUBYLIB"]}",
"PATH" => "#{repo_root}/decidim-generators/exe:#{ENV["PATH"]}",
"COMMAND_NAME" => example.full_description.tr(" ", "_")
}
else
{}
end
end
let(:result) do
Bundler.with_original_env { GemManager.capture(command, env: env) }
end
# rubocop:disable RSpec/BeforeAfterAll
before(:all) do
Decidim::GemManager.install_all(out: File::NULL)
end
after(:all) do
Decidim::GemManager.uninstall_all(out: File::NULL)
end
# rubocop:enable RSpec/BeforeAfterAll
context "with an application" do
let(:test_app) { "spec/generator_test_app" }
after { FileUtils.rm_rf(test_app) }
shared_examples_for "a new production application" do
it "includes optional plugins commented out in Gemfile" do
expect(result[1]).to be_success, result[0]
expect(File.read("#{test_app}/Gemfile"))
.to match(/^# gem "decidim-initiatives"/)
.and match(/^# gem "decidim-consultations"/)
.and match(/^# gem "decidim-elections"/)
.and match(/^# gem "decidim-conferences"/)
.and match(/^# gem "decidim-templates"/)
end
end
shared_examples_for "a new development application" do
it "includes optional plugins uncommented in Gemfile" do
expect(result[1]).to be_success, result[0]
expect(File.read("#{test_app}/Gemfile"))
.to match(/^gem "decidim-initiatives"/)
.and match(/^gem "decidim-consultations"/)
.and match(/^gem "decidim-elections"/)
.and match(/^gem "decidim-conferences"/)
.and match(/^gem "decidim-templates"/)
# Checks that every table from a migration is included in the generated schema
schema = File.read("#{test_app}/db/schema.rb")
tables = []
dropped = []
Decidim::GemManager.plugins.each do |plugin|
Dir.glob("#{plugin}db/migrate/*.rb").each do |migration|
lines = File.readlines(migration)
tables.concat(lines.filter { |line| line.match? "create_table" }.map { |line| line.match(/(:)([a-z_0-9]+)/)[2] })
dropped.concat(lines.filter { |line| line.match? "drop_table" }.map { |line| line.match(/(:)([a-z_0-9]+)/)[2] })
tables.concat(lines.filter { |line| line.match? "rename_table" }.map { |line| line.match(/(, :)([a-z_0-9]+)/)[2] })
dropped.concat(lines.filter { |line| line.match? "rename_table" }.map { |line| line.match(/(:)([a-z_0-9]+)/)[2] })
end
end
tables.each do |table|
next if dropped.include? table
expect(schema).to match(/create_table "#{table}"|create_table :#{table}/)
end
end
end
context "without flags" do
let(:command) { "decidim #{test_app}" }
it_behaves_like "a new production application"
end
context "with --edge flag" do
let(:command) { "decidim --edge #{test_app}" }
it_behaves_like "a new production application"
end
context "with --branch flag" do
let(:default_branch) { "upgrade/rails-6.1" }
let(:command) { "decidim --branch #{default_branch} #{test_app}" }
it_behaves_like "a new production application"
end
context "with --path flag" do
let(:command) { "decidim --path #{repo_root} #{test_app}" }
it_behaves_like "a new production application"
end
context "with a full featured application" do
let(:command) { "decidim #{test_app} --recreate_db --demo" }
it_behaves_like "a new development application"
end
context "with a development application" do
let(:command) { "decidim --path #{repo_root} #{test_app} --recreate_db --seed_db --demo" }
it_behaves_like "a new development application"
end
end
context "with a component" do
let(:test_component) { "dummy_component" }
let(:command) { "decidim --component #{test_component}" }
after { FileUtils.rm_rf("decidim-module-#{test_component}") }
it "suceeeds" do
expect(result[1]).to be_success, result[0]
end
end
private
def repo_root
File.expand_path(File.join("..", ".."), __dir__)
end
end
end