Skip to content

Commit

Permalink
Resolved pointing out from RuboCop.
Browse files Browse the repository at this point in the history
  • Loading branch information
M-Yamashita01 committed Feb 25, 2023
1 parent 2bd68c3 commit 8e0709b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
22 changes: 11 additions & 11 deletions lib/terraform_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,27 @@ class TerraformReader
].freeze

def self.open_file(terraform_json_file_path:)
json_file = File.open(terraform_json_file_path)
terraform_json = JSON.load(json_file)
json_file = File.read(terraform_json_file_path)
terraform_json = JSON.parse(json_file)

new(terraform_json: terraform_json)
end

def read_members
begin
planned_values = @terraform_json["planned_values"]
root_module = planned_values["root_module"]
resources = root_module["resources"]
planned_values = @terraform_json['planned_values']
root_module = planned_values['root_module']
resources = root_module['resources']

usernames = []
resources.each do |resource|
values = resource["values"]
usernames << values["username"]
values = resource['values']
usernames << values['username']
end
rescue => error
logger.error(error.message)
logger.error(error.backtrace.join("\n"))
raise StandardError.new("The terraform json file is invalid.")
rescue StandardError => e
logger.error(e.message)
logger.error(e.backtrace.join("\n"))
raise StandardError, 'The terraform json file is invalid.'
end

usernames.uniq
Expand Down
15 changes: 7 additions & 8 deletions spec/terraform_reader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,27 @@
require 'spec_helper'

RSpec.describe TerraformReader do

describe 'open_file' do
it 'get TerraformReader instance' do
terraform_json_file_path = File.expand_path("#{__dir__}/fixtures/terraform_valid_json.json")
terraform_reader = TerraformReader.open_file(terraform_json_file_path: terraform_json_file_path)
expect(terraform_reader.class.name).to eq("TerraformReader")
terraform_reader = described_class.open_file(terraform_json_file_path: terraform_json_file_path)
expect(terraform_reader.class.name).to eq('TerraformReader')
end
end

describe '#usernames' do
it 'get members list' do
terraform_json_file_path = File.expand_path("#{__dir__}/fixtures/terraform_valid_json.json")
terraform_reader = TerraformReader.open_file(terraform_json_file_path: terraform_json_file_path)
terraform_reader = described_class.open_file(terraform_json_file_path: terraform_json_file_path)
usernames = terraform_reader.read_members
expect(usernames).to contain_exactly("Membership1", "Membership2", "Membership3")
expect(usernames).to contain_exactly('Membership1', 'Membership2', 'Membership3')
end

context 'when invalid json file' do
it "raise StandardError" do
it 'raise StandardError' do
terraform_json_file_path = File.expand_path("#{__dir__}/fixtures/terraform_invalid_json.json")
terraform_reader = TerraformReader.open_file(terraform_json_file_path: terraform_json_file_path)
expect{terraform_reader.read_members}.to raise_error StandardError
terraform_reader = described_class.open_file(terraform_json_file_path: terraform_json_file_path)
expect { terraform_reader.read_members }.to raise_error StandardError
end
end
end
Expand Down

0 comments on commit 8e0709b

Please sign in to comment.