|
24 | 24 | FileUtils.rm_rf GENERATOR_PLAYGROUND_PATH
|
25 | 25 | end
|
26 | 26 |
|
27 |
| - it "generates terraform config files", :aggregate_failures do |
28 |
| - config_file_paths.each { |config_file_path| expect(config_file_path).not_to exist } |
| 27 | + shared_examples "generates terraform config files" do |
| 28 | + specify do |
| 29 | + all_config_paths.each { |path| expect(path).not_to exist } |
29 | 30 |
|
30 |
| - expect(result[:status]).to eq(0) |
| 31 | + expect(result[:status]).to eq(ExitCode::SUCCESS) |
| 32 | + expect(result[:stderr]).to err_msg ? include(err_msg) : be_empty |
31 | 33 |
|
32 |
| - expect(config_file_paths).to all(exist) |
| 34 | + expect(expected_config_paths).to all(exist) |
| 35 | + (all_config_paths - expected_config_paths).each { |path| expect(path).not_to exist } |
| 36 | + end |
| 37 | + end |
| 38 | + |
| 39 | + shared_examples "does not generate any terraform config files" do |err_msg| |
| 40 | + it "fails with an error" do |
| 41 | + all_config_paths.each { |path| expect(path).not_to exist } |
| 42 | + |
| 43 | + expect(result[:status]).to eq(ExitCode::ERROR_DEFAULT) |
| 44 | + expect(result[:stderr]).to include(err_msg) |
| 45 | + |
| 46 | + all_config_paths.each { |path| expect(path).not_to exist } |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + it_behaves_like "generates terraform config files" do |
| 51 | + let(:expected_config_paths) { all_config_paths } |
| 52 | + let(:err_msg) { nil } |
33 | 53 | end
|
34 | 54 |
|
35 | 55 | context "when templates folder is empty" do
|
|
39 | 59 | allow_any_instance_of(TemplateParser).to receive(:template_dir).and_return(template_dir) # rubocop:disable RSpec/AnyInstance
|
40 | 60 | end
|
41 | 61 |
|
42 |
| - it "generates only common config files" do |
43 |
| - config_file_paths.each { |config_file_path| expect(config_file_path).not_to exist } |
44 |
| - |
45 |
| - expect(result[:stderr]).to include("No templates found in #{template_dir}") |
46 |
| - |
47 |
| - expect(common_config_files).to all(exist) |
48 |
| - app_config_files.each { |config_file_path| expect(config_file_path).not_to exist } |
| 62 | + it_behaves_like "generates terraform config files" do |
| 63 | + let(:expected_config_paths) { provider_config_paths } |
| 64 | + let(:err_msg) { "No templates found in #{template_dir}" } |
49 | 65 | end
|
50 | 66 | end
|
51 | 67 |
|
|
54 | 70 | allow_any_instance_of(TemplateParser).to receive(:parse).and_raise("error") # rubocop:disable RSpec/AnyInstance
|
55 | 71 | end
|
56 | 72 |
|
57 |
| - it "generates only common config files" do |
58 |
| - config_file_paths.each { |config_file_path| expect(config_file_path).not_to exist } |
59 |
| - |
60 |
| - expect(result[:stderr]).to include("Error parsing templates: error") |
61 |
| - |
62 |
| - expect(common_config_files).to all(exist) |
63 |
| - app_config_files.each { |config_file_path| expect(config_file_path).not_to exist } |
| 73 | + it_behaves_like "generates terraform config files" do |
| 74 | + let(:expected_config_paths) { provider_config_paths } |
| 75 | + let(:err_msg) { "Error parsing templates: error" } |
64 | 76 | end
|
65 | 77 | end
|
66 | 78 |
|
67 | 79 | context "when --dir option is outside of project dir" do
|
68 | 80 | let(:options) { ["-a", app, "--dir", GEM_TEMP_PATH.join("path-outside-of-project").to_s] }
|
69 | 81 |
|
70 |
| - it "aborts command execution" do |
71 |
| - expect(result[:status]).to eq(ExitCode::ERROR_DEFAULT) |
72 |
| - expect(result[:stderr]).to include( |
73 |
| - "Directory to save terraform configuration files cannot be outside of current directory" |
74 |
| - ) |
75 |
| - end |
| 82 | + it_behaves_like "does not generate any terraform config files", |
| 83 | + "Directory to save terraform configuration files cannot be outside of current directory" |
76 | 84 | end
|
77 | 85 |
|
78 | 86 | context "when terraform config directory creation fails" do
|
79 | 87 | before do
|
80 | 88 | allow(FileUtils).to receive(:mkdir_p).and_raise("error")
|
81 | 89 | end
|
82 | 90 |
|
83 |
| - it "aborts command execution" do |
84 |
| - expect(result[:status]).to eq(ExitCode::ERROR_DEFAULT) |
85 |
| - expect(result[:stderr]).to include("error") |
86 |
| - end |
| 91 | + it_behaves_like "does not generate any terraform config files", "error" |
87 | 92 | end
|
88 | 93 |
|
89 |
| - def config_file_paths |
90 |
| - common_config_files + app_config_files |
| 94 | + def all_config_paths |
| 95 | + provider_config_paths + template_config_paths |
91 | 96 | end
|
92 | 97 |
|
93 |
| - def common_config_files |
94 |
| - [TERRAFORM_CONFIG_DIR_PATH.join("providers.tf")] |
| 98 | + def provider_config_paths |
| 99 | + %w[required_providers.tf providers.tf].map { |filename| config_path(filename) } |
95 | 100 | end
|
96 | 101 |
|
97 |
| - def app_config_files |
98 |
| - %w[gvc.tf identities.tf secrets.tf policies.tf].map do |config_file_path| |
99 |
| - TERRAFORM_CONFIG_DIR_PATH.join(app, config_file_path) |
100 |
| - end |
| 102 | + def template_config_paths |
| 103 | + %w[gvc.tf identities.tf secrets.tf policies.tf].map { |filename| config_path(filename) } |
| 104 | + end |
| 105 | + |
| 106 | + def config_path(name) |
| 107 | + TERRAFORM_CONFIG_DIR_PATH.join(app, name) |
101 | 108 | end
|
102 | 109 | end
|
0 commit comments