Skip to content

Commit

Permalink
Apply changes related to the rubocop rules.
Browse files Browse the repository at this point in the history
  • Loading branch information
rogerluan committed Oct 21, 2023
1 parent 34b6dff commit 835d321
Show file tree
Hide file tree
Showing 17 changed files with 183 additions and 169 deletions.
14 changes: 7 additions & 7 deletions lib/arkana/helpers/swift_template_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
module SwiftTemplateHelper
def self.swift_type(type)
case type
when :string then "String"
when :boolean then "Bool"
when :integer then "Int"
else raise "Unknown variable type '#{type}' received.'"
when :string then "String"
when :boolean then "Bool"
when :integer then "Int"
else raise "Unknown variable type '#{type}' received.'"
end
end

def self.protocol_getter(declaration_strategy)
case declaration_strategy
when "lazy var" then "mutating get"
when "var", "let" then "get"
else raise "Unknown declaration strategy '#{declaration_strategy}' received.'"
when "lazy var" then "mutating get"
when "var", "let" then "get"
else raise "Unknown declaration strategy '#{declaration_strategy}' received.'"
end
end
end
12 changes: 6 additions & 6 deletions lib/arkana/models/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ module Type

def self.new(string_value:)
case string_value
when "true", "false"
BOOLEAN
when /^\d+$/
INTEGER
else
STRING
when "true", "false"
BOOLEAN
when /^\d+$/
INTEGER
else
STRING
end
end
end
9 changes: 5 additions & 4 deletions spec/arkana_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
let(:config_filepath) { "spec/fixtures/arkana-fixture.yml" }
let(:arguments) { Arguments.new }
let(:config) { ConfigParser.parse(arguments) }

before { ARGV.replace(["--config-filepath", config_filepath]) }

context "when one or more env vars are missing" do
it "should raise error" do
expect { Arkana.run(arguments) }.to raise_error(/Secret '(?:.*)' was declared but couldn't be found in the environment variables nor in the specified dotenv file./)
it "raises error" do
expect { described_class.run(arguments) }.to raise_error(/Secret '(?:.*)' was declared but couldn't be found in the environment variables nor in the specified dotenv file./)
end
end

Expand All @@ -20,9 +21,9 @@
end
end

it "should call SwiftCodeGenerator.generate" do
it "calls SwiftCodeGenerator.generate" do
expect(SwiftCodeGenerator).to receive(:generate)
Arkana.run(arguments)
described_class.run(arguments)
end
end
end
Expand Down
36 changes: 20 additions & 16 deletions spec/config_parser_spec.rb
Original file line number Diff line number Diff line change
@@ -1,38 +1,40 @@
# frozen_string_literal: true

RSpec.describe ConfigParser do
subject { ConfigParser.parse(Arguments.new) }
subject { described_class.parse(Arguments.new) }

describe "#parse" do
before { ARGV.replace(["--config-filepath", "spec/fixtures/arkana-fixture.yml"]) }

describe "when yaml file exists" do
it "should not fail" do
expect { ConfigParser.parse(Arguments.new) }.to_not raise_error
it "does not fail" do
expect { described_class.parse(Arguments.new) }.not_to raise_error
end
end

describe "when yaml file doesn't exist" do
let(:invalid_path) { "path/to/limbo" }

before { ARGV.replace(["--config-filepath", invalid_path]) }

it "should raise an error with user friendly message" do
expect { ConfigParser.parse(Arguments.new) }.to raise_error(/No such file or directory (?:.*) #{invalid_path}/)
it "raises an error with user friendly message" do
expect { described_class.parse(Arguments.new) }.to raise_error(/No such file or directory (?:.*) #{invalid_path}/)
end
end

describe "#current_flavor" do
describe "when flavor is specified in arguments" do
let(:flavor) { "frootloops" }

before { ARGV << "--flavor" << flavor }

it "should be the same as the flavor specified" do
it "is the same as the flavor specified" do
expect(subject.current_flavor).to eq flavor
end
end

describe "when flavor is not specified in arguments" do
it "should be nil" do
it "is nil" do
expect(subject.current_flavor).to be_nil
end
end
Expand All @@ -42,21 +44,23 @@
context "when dotenv_filepath is specified" do
context "when it exists" do
let(:dotenv_filepath) { "spec/fixtures/dotenv.fixture" }

before { ARGV << "--dotenv-filepath" << dotenv_filepath }

it "should not log a warning and not raise an error" do
expect(UI).to_not receive(:warn)
expect { subject }.to_not raise_error
it "does not log a warning and not raise an error" do
expect(UI).not_to receive(:warn)
expect { subject }.not_to raise_error
end
end

context "when it doesn't exist" do
let(:dotenv_filepath) { "path/to/limbo" }

before { ARGV << "--dotenv-filepath" << dotenv_filepath }

it "should log a warning and not raise an error" do
it "logs a warning and not raise an error" do
expect(UI).to receive(:warn).with("Dotenv file was specified but couldn't be found at '#{dotenv_filepath}'")
expect { subject }.to_not raise_error
expect { subject }.not_to raise_error
end
end
end
Expand All @@ -65,17 +69,17 @@
let(:default_fallback_dotenv_filepath) { ".env" }

context "when a file exists at the default fallback dotenv filepath" do
it "should have dotenv_filepath equal to the default fallback dotenv filepath" do
it "has dotenv_filepath equal to the default fallback dotenv filepath" do
expect(File).to receive(:exist?).with(default_fallback_dotenv_filepath).twice.and_return(true)
expect(UI).to_not receive(:warn)
expect(UI).not_to receive(:warn)
expect(subject.dotenv_filepath).to eq default_fallback_dotenv_filepath
end
end

context "when a file doesn't exist at the default fallback dotenv filepath" do
it "should have dotenv_filepath be nil" do
it "has dotenv_filepath be nil" do
expect(File).to receive(:exist?).with(default_fallback_dotenv_filepath).and_return(false)
expect(UI).to_not receive(:warn)
expect(UI).not_to receive(:warn)
expect(subject.dotenv_filepath).to be_nil
end
end
Expand Down
16 changes: 8 additions & 8 deletions spec/dotenv_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,31 @@
context "when current_flavor is not passed" do
let(:current_flavor) { nil }

it "should load dotenv file from dotenv_filepath" do
it "loads dotenv file from dotenv_filepath" do
expect(Dotenv).to receive(:load).with(dotenv_filepath).once
DotenvHelper.load(config)
described_class.load(config)
end
end

context "when current_flavor is passed" do
it "should load dotenv files and flavor-specific env vars should override regular dotenv env vars" do
it "loads dotenv files and flavor-specific env vars should override regular dotenv env vars" do
expect(Dotenv).to receive(:load).with(dotenv_filepath).once
expect(Dotenv).to receive(:load).with(DotenvHelper.flavor_dotenv_filepath(config)).once
DotenvHelper.load(config)
expect(Dotenv).to receive(:load).with(described_class.flavor_dotenv_filepath(config)).once
described_class.load(config)
# NOTE: I couldn't make this work, not sure if it's possible:
# expect(ENV["DOTENV_KEY"]).to eq "value from flavor dotenv"
end
end
end

describe ".flavor_dotenv_filepath" do
subject { DotenvHelper.flavor_dotenv_filepath(config) }
subject { described_class.flavor_dotenv_filepath(config) }

it "should be in the same directory as the dotenv_filepath" do
it "is in the same directory as the dotenv_filepath" do
expect(subject).to start_with("fixtures")
end

it "should have a format of '.env.' followed by lowercased flavor" do
it "has a format of '.env.' followed by lowercased flavor" do
expect(subject).to end_with("/.env.#{current_flavor.downcase}")
end
end
Expand Down
42 changes: 24 additions & 18 deletions spec/encoder_spec.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# frozen_string_literal: true

RSpec.describe Encoder do
subject { described_class.encode!(keys: keys, salt: salt, current_flavor: current_flavor, environments: environments) }

let(:salt) { SaltGenerator.generate }
let(:environments) { [] }
let(:current_flavor) { nil }
subject { Encoder.encode!(keys: keys, salt: salt, current_flavor: current_flavor, environments: environments) }

describe ".encode!" do
context "when keys is empty" do
let(:keys) { [] }

it "should return empty array" do
it "returns empty array" do
expect(subject.map(&:key)).to be_empty
end
end
Expand All @@ -29,16 +30,16 @@
allow(ENV).to receive(:[]).with(existing_key2).and_return(existing_value2)
end

it "should not raise error" do
expect { subject }.to_not raise_error
it "does not raise error" do
expect { subject }.not_to raise_error
end

it "should return array with matching keys, values that are not the same as the stored value, and types" do
it "returns array with matching keys, values that are not the same as the stored value, and types" do
expect(subject[0].key).to eq existing_key
expect(subject[1].key).to eq existing_key2

expect(subject[0].encoded_value).to_not eq existing_value
expect(subject[1].encoded_value).to_not eq existing_value2
expect(subject[0].encoded_value).not_to eq existing_value
expect(subject[1].encoded_value).not_to eq existing_value2

expect(subject[0].type).to eq :string
expect(subject[1].type).to eq :boolean
Expand All @@ -54,26 +55,28 @@
allow(ENV).to receive(:[]).with(missing_key).and_return(nil)
end

it "should raise error" do
it "raises error" do
expect { subject }.to raise_error(/Secret '(?:.*)' was declared but couldn't be found in the environment variables nor in the specified dotenv file./)
end
end
end
end

describe "#find_secret!" do
subject { described_class.find_secret!(key: key, current_flavor: current_flavor) }

let(:key) { "LoremIpsum" }
subject { Encoder.find_secret!(key: key, current_flavor: current_flavor) }

context "when current_flavor is passed" do
let(:current_flavor) { "Ipsum" }
let(:flavor_key) { "#{current_flavor.capitalize_first_letter}#{key}" }

context "when ENV contains the flavor-specific key" do
let(:value) { "value" }

before { allow(ENV).to receive(:[]).with(flavor_key).and_return(value) }

it "should return the value from ENV" do
it "returns the value from ENV" do
expect(subject).to eq value
end
end
Expand All @@ -83,17 +86,18 @@

context "when ENV contains the key" do
let(:value) { "value" }

before { allow(ENV).to receive(:[]).with(key).and_return(value) }

it "should return the value from ENV" do
it "returns the value from ENV" do
expect(subject).to eq value
end
end

context "when ENV doesn't contain the key" do
before { allow(ENV).to receive(:[]).with(key).and_return(nil) }

it "should raise" do
it "raises" do
expect { subject }.to raise_error(/Secret '#{flavor_key}' was declared but couldn't be found in the environment variables nor in the specified dotenv file./)
end
end
Expand All @@ -105,47 +109,49 @@

context "when ENV contains the key" do
let(:value) { "value" }

before { allow(ENV).to receive(:[]).with(key).and_return(value) }

it "should return the value from ENV" do
it "returns the value from ENV" do
expect(subject).to eq value
end
end

context "when ENV doesn't contain the key" do
before { allow(ENV).to receive(:[]).with(key).and_return(nil) }

it "should raise" do
it "raises" do
expect { subject }.to raise_error(/Secret '#{key}' was declared but couldn't be found in the environment variables nor in the specified dotenv file./)
end
end
end
end

describe "#protocol_key" do
subject { described_class.protocol_key(key: key, environments: environment ? [environment] : []) }

let(:key) { "LoremIpsum" }
subject { Encoder.protocol_key(key: key, environments: environment ? [environment] : []) }

context "when the key passed has a suffix of one of the environments" do
let(:environment) { "Ipsum" }

it "should return the key without the environment suffix" do
it "returns the key without the environment suffix" do
expect(subject).to eq key.delete_suffix(environment)
end
end

context "when the key passed doesn't have a suffix of one of the environments" do
let(:environment) { "Dolor" }

it "should return the same key passed" do
it "returns the same key passed" do
expect(subject).to eq key
end
end

context "when no environments are passed" do
let(:environment) { nil }

it "should return the same key passed" do
it "returns the same key passed" do
expect(subject).to eq key
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/helpers/string_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
describe "#camel_case" do
subject { "FooBar" }

it "should lowercase the first letter" do
it "lowercases the first letter" do
expect(subject.camel_case).to eq "fooBar"
end
end

describe "#capitalize_first_letter" do
subject { "fOOBaR" }

it "should capitalize the first letter and not change anything else in the rest of the string" do
it "capitalizes the first letter and not change anything else in the rest of the string" do
expect(subject.capitalize_first_letter).to eq "FOOBaR"
end
end
Expand Down
Loading

0 comments on commit 835d321

Please sign in to comment.