diff --git a/Rakefile b/Rakefile index 5f46fd7..18b2ca5 100644 --- a/Rakefile +++ b/Rakefile @@ -15,27 +15,35 @@ task default: %i[spec rubocop] desc "Generates Swift source code and run its unit tests." task :test_swift do - config_file = File.absolute_path("spec/fixtures/swift-tests.yml") + config_file_default = File.absolute_path("spec/fixtures/swift-tests.yml") + config_file_no_infer_types = File.absolute_path("spec/fixtures/swift-tests-with-no-infer-types.yml") + config_file_array = [config_file_default, config_file_no_infer_types] dotenv_file = File.absolute_path("spec/fixtures/.env.fruitloops") - with_temp_dir do |temp_dir| - puts "Current working directory: #{temp_dir}" - sh("ARKANA_RUNNING_CI_INTEGRATION_TESTS=true arkana --config-filepath #{config_file} --dotenv-filepath #{dotenv_file} --include-environments dev,staging") - Dir.chdir("tests/MySecrets") - sh("swift test") + config_file_array.each do |config_file| + with_temp_dir do |temp_dir| + puts "Current working directory: #{temp_dir}" + sh("ARKANA_RUNNING_CI_INTEGRATION_TESTS=true arkana --config-filepath #{config_file} --dotenv-filepath #{dotenv_file} --include-environments dev,staging") + Dir.chdir("tests/MySecrets") + sh("swift test") + end end end desc "Generates Kotlin source code and run its unit tests." task :test_kotlin do - config_file = File.absolute_path("spec/fixtures/kotlin-tests.yml") + config_file_default = File.absolute_path("spec/fixtures/kotlin-tests.yml") + config_file_no_infer_types = File.absolute_path("spec/fixtures/kotlin-tests-with-no-infer-types.yml") + config_file_array = [config_file_default, config_file_no_infer_types] dotenv_file = File.absolute_path("spec/fixtures/.env.fruitloops") directory_to_copy = File.absolute_path("spec/fixtures/kotlin") - with_temp_dir do |temp_dir| - puts "Current working directory: #{temp_dir}" - FileUtils.copy_entry(directory_to_copy, "tests") - sh("ARKANA_RUNNING_CI_INTEGRATION_TESTS=true arkana --lang kotlin --config-filepath #{config_file} --dotenv-filepath #{dotenv_file} --include-environments dev,staging") - Dir.chdir("tests") - sh("./gradlew test") + config_file_array.each do |config_file| + with_temp_dir do |temp_dir| + puts "Current working directory: #{temp_dir}" + FileUtils.copy_entry(directory_to_copy, "tests") + sh("ARKANA_RUNNING_CI_INTEGRATION_TESTS=true arkana --lang kotlin --config-filepath #{config_file} --dotenv-filepath #{dotenv_file} --include-environments dev,staging") + Dir.chdir("tests") + sh("./gradlew test") + end end end diff --git a/lib/arkana.rb b/lib/arkana.rb index 589754a..684651d 100644 --- a/lib/arkana.rb +++ b/lib/arkana.rb @@ -23,12 +23,14 @@ def self.run(arguments) salt: salt, current_flavor: config.current_flavor, environments: config.environments, + should_infer_types: config.should_infer_types, ) global_secrets = Encoder.encode!( keys: config.global_secrets, salt: salt, current_flavor: config.current_flavor, environments: config.environments, + should_infer_types: config.should_infer_types, ) rescue StandardError => e # TODO: Improve this by creating an Env/Debug helper diff --git a/lib/arkana/encoder.rb b/lib/arkana/encoder.rb index 6aa3653..ae1fa03 100644 --- a/lib/arkana/encoder.rb +++ b/lib/arkana/encoder.rb @@ -7,11 +7,11 @@ module Encoder # Fetches values of each key from ENV, and encodes them using the given salt. # # @return [Secret[]] an array of Secret objects, which contain their keys and encoded values. - def self.encode!(keys:, salt:, current_flavor:, environments:) + def self.encode!(keys:, salt:, current_flavor:, environments:, should_infer_types:) keys.map do |key| secret = find_secret!(key: key, current_flavor: current_flavor) encoded_value = encode(secret, salt.raw) - secret_type = Type.new(string_value: secret) + secret_type = should_infer_types ? Type.new(string_value: secret) : Type.default_type protocol_key = protocol_key(key: key, environments: environments) Secret.new(key: key, protocol_key: protocol_key, encoded_value: encoded_value, type: secret_type) end diff --git a/lib/arkana/models/config.rb b/lib/arkana/models/config.rb index 83d496a..377e725 100644 --- a/lib/arkana/models/config.rb +++ b/lib/arkana/models/config.rb @@ -36,6 +36,8 @@ class Config attr_reader :kotlin_jvm_toolchain_version # @returns [boolean] attr_reader :is_kotlin_multiplatform_module + # @returns [boolean] + attr_reader :should_infer_types # @returns [string] attr_accessor :current_flavor @@ -68,6 +70,8 @@ def initialize(yaml) @kotlin_jvm_toolchain_version = yaml["kotlin_jvm_toolchain_version"] || 11 @is_kotlin_multiplatform_module = yaml["is_kotlin_multiplatform_module"] @is_kotlin_multiplatform_module = false if @should_generate_gradle_build_file.nil? + @should_infer_types = yaml["should_infer_types"] + @should_infer_types = true if @should_infer_types.nil? end # rubocop:enable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity diff --git a/lib/arkana/models/type.rb b/lib/arkana/models/type.rb index f700616..c04f57a 100644 --- a/lib/arkana/models/type.rb +++ b/lib/arkana/models/type.rb @@ -20,4 +20,9 @@ def self.new(string_value:) STRING end end + + # The default type to use when we shouldn't be inferring types. + def self.default_type + STRING + end end diff --git a/spec/encoder_spec.rb b/spec/encoder_spec.rb index cb744a0..ae23bd2 100644 --- a/spec/encoder_spec.rb +++ b/spec/encoder_spec.rb @@ -1,11 +1,12 @@ # frozen_string_literal: true RSpec.describe Encoder do - subject { described_class.encode!(keys: keys, salt: salt, current_flavor: current_flavor, environments: environments) } + subject { described_class.encode!(keys: keys, salt: salt, current_flavor: current_flavor, environments: environments, should_infer_types: should_infer_types) } let(:salt) { SaltGenerator.generate } let(:environments) { [] } let(:current_flavor) { nil } + let(:should_infer_types) { true } describe ".encode!" do context "when keys is empty" do diff --git a/spec/fixtures/kotlin-tests-with-no-infer-types.yml b/spec/fixtures/kotlin-tests-with-no-infer-types.yml new file mode 100644 index 0000000..d5e39f7 --- /dev/null +++ b/spec/fixtures/kotlin-tests-with-no-infer-types.yml @@ -0,0 +1,32 @@ +namespace: 'MySecrets' +result_path: 'tests' +should_generate_unit_tests: true +should_generate_gradle_build_file: false +global_secrets: +- BoolAsStringTrueKey +- BoolAsStringFalseKey +- BoolAsBoolTrueKey +- BoolAsBoolFalseKey +- IntAsStringKey +- IntAsNumberKey +- IntWithLeadingZeroesAsStringKey +- IntWithLeadingZeroesAsNumberKey +- MassiveIntAsStringKey +- MassiveIntAsNumberKey +- NegativeIntAsStringKey +- NegativeIntAsNumberKey +- FloatAsStringKey +- FloatAsNumberKey +- SecretWithDollarSignEscapedAndAndNoQuotesKey +- SecretWithDollarSignEscapedAndDoubleQuoteKey +- SecretWithDollarSignNotEscapedAndSingleQuoteKey +- SecretWithDollarSignNotEscapedAndDoubleQuotesKey +- SecretWithDollarSignNotEscapedAndNoQuotesKey +- SecretWithWeirdCharactersKey +environments: + - dev + - staging + - prod +environment_secrets: + - ServiceKey + - Server diff --git a/spec/fixtures/swift-tests-with-no-infer-types.yml b/spec/fixtures/swift-tests-with-no-infer-types.yml new file mode 100644 index 0000000..c0a9bff --- /dev/null +++ b/spec/fixtures/swift-tests-with-no-infer-types.yml @@ -0,0 +1,36 @@ +import_name: 'MySecrets' +namespace: 'MySecrets' +pod_name: 'MySecrets' +result_path: 'tests' +swift_declaration_strategy: lazy var +should_generate_unit_tests: true +should_inter_types: false +package_manager: spm +global_secrets: +- BoolAsStringTrueKey +- BoolAsStringFalseKey +- BoolAsBoolTrueKey +- BoolAsBoolFalseKey +- IntAsStringKey +- IntAsNumberKey +- IntWithLeadingZeroesAsStringKey +- IntWithLeadingZeroesAsNumberKey +- MassiveIntAsStringKey +- MassiveIntAsNumberKey +- NegativeIntAsStringKey +- NegativeIntAsNumberKey +- FloatAsStringKey +- FloatAsNumberKey +- SecretWithDollarSignEscapedAndAndNoQuotesKey +- SecretWithDollarSignEscapedAndDoubleQuoteKey +- SecretWithDollarSignNotEscapedAndSingleQuoteKey +- SecretWithDollarSignNotEscapedAndDoubleQuotesKey +- SecretWithDollarSignNotEscapedAndNoQuotesKey +- SecretWithWeirdCharactersKey +environments: + - dev + - staging + - prod +environment_secrets: + - ServiceKey + - Server diff --git a/spec/kotlin_code_generator_spec.rb b/spec/kotlin_code_generator_spec.rb index 3c22cae..e28cdda 100644 --- a/spec/kotlin_code_generator_spec.rb +++ b/spec/kotlin_code_generator_spec.rb @@ -9,6 +9,7 @@ salt: salt, current_flavor: config.current_flavor, environments: config.environments, + should_infer_types: config.should_infer_types, ) end @@ -18,6 +19,7 @@ salt: salt, current_flavor: config.current_flavor, environments: config.environments, + should_infer_types: config.should_infer_types, ) end diff --git a/spec/models/template_arguments_spec.rb b/spec/models/template_arguments_spec.rb index e54916a..61ec2bd 100644 --- a/spec/models/template_arguments_spec.rb +++ b/spec/models/template_arguments_spec.rb @@ -18,6 +18,7 @@ salt: salt, current_flavor: config.current_flavor, environments: config.environments, + should_infer_types: config.should_infer_types, ) end @@ -27,6 +28,7 @@ salt: salt, current_flavor: config.current_flavor, environments: config.environments, + should_infer_types: config.should_infer_types, ) end diff --git a/spec/swift_code_generator_spec.rb b/spec/swift_code_generator_spec.rb index d4f6d74..b96d00e 100644 --- a/spec/swift_code_generator_spec.rb +++ b/spec/swift_code_generator_spec.rb @@ -9,6 +9,7 @@ salt: salt, current_flavor: config.current_flavor, environments: config.environments, + should_infer_types: config.should_infer_types, ) end @@ -18,6 +19,7 @@ salt: salt, current_flavor: config.current_flavor, environments: config.environments, + should_infer_types: config.should_infer_types, ) end diff --git a/template.yml b/template.yml index 2b83c93..afcb186 100644 --- a/template.yml +++ b/template.yml @@ -14,6 +14,7 @@ kotlin_sources_path: 'java' # Optional. The path for the generated Kotlin classe should_generate_gradle_build_file: true # Optional. Whether a build.gradle file should be generated, when running the Kotlin generator. One of: true, false. Defaults to true. is_kotlin_multiplatform_module: true # Optional. Whether the generated Kotlin module is a multiplatform module. One of: true, false. Defaults to false. kotlin_jvm_toolchain_version: 11 # Optional. The kotlin JVM toolchain JDK version to be used in the generated build.gradle file. Defaults to 11. +should_infer_types: true # Optional. Whether type inference should be done based on the values of the secrets. When false, the type will always be string for all languages. One of: true, false. Defaults to true. environments: # Optional. List of environments that will be used to generate secret keys when you have keys that are different between environments (e.g. debug/staging/prod). Defaults to empty. - Debug - Release