From e118c0407f8451ba8c719d40f5bce93e67a8411c Mon Sep 17 00:00:00 2001 From: J2EEbbesen Date: Mon, 14 Oct 2024 10:53:42 -0500 Subject: [PATCH] Environment variable check (#20) * Refactoring environment vars check and adding to gem * Logging changes for next release --- CHANGELOG.md | 4 ++++ Gemfile.lock | 2 +- lib/sheet_zoukas.rb | 4 ++++ lib/sheet_zoukas/utils.rb | 17 +++++++++++++ lib/sheet_zoukas/version.rb | 2 +- spec/sheet_zoukas/utils_spec.rb | 42 +++++++++++++++++++++++++++++++++ spec/sheet_zoukas_spec.rb | 12 ++++++++++ spec/spec_helper.rb | 21 ++++------------- 8 files changed, 85 insertions(+), 19 deletions(-) create mode 100644 lib/sheet_zoukas/utils.rb create mode 100644 spec/sheet_zoukas/utils_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 08a3b02..0c136cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ ## [0.1.0] - 2024-10-13 - Initial release v0.1.0 + +## [0.2.0] - UNRELEASED + +- Added check for required environment variables before call to Google Sheets API diff --git a/Gemfile.lock b/Gemfile.lock index bee8caf..6c0f877 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - sheet_zoukas (0.1.0) + sheet_zoukas (0.2.0) google-apis-sheets_v4 GEM diff --git a/lib/sheet_zoukas.rb b/lib/sheet_zoukas.rb index fbe41bc..f0a724d 100644 --- a/lib/sheet_zoukas.rb +++ b/lib/sheet_zoukas.rb @@ -3,13 +3,17 @@ require_relative 'sheet_zoukas/data_converter' require_relative 'sheet_zoukas/google_sheets' require_relative 'sheet_zoukas/version' +require_relative 'sheet_zoukas/utils' # Retrieve Google Sheets data module SheetZoukas class Error < StandardError; end + REQUIRED_VARS = %w[GOOGLE_ACCOUNT_TYPE GOOGLE_API_KEY GOOGLE_CLIENT_EMAIL GOOGLE_CLIENT_ID GOOGLE_PRIVATE_KEY].freeze class << self def retrieve_sheet_json(sheet_id, tab_name, range = nil) + exit 1 unless SheetZoukas::Utils.vars_present?(REQUIRED_VARS, 'required for Google Sheets API calls') + sheet = GoogleSheets.new data = sheet.retrieve_sheet(sheet_id, tab_name, range) DataConverter.new(data.values).convert diff --git a/lib/sheet_zoukas/utils.rb b/lib/sheet_zoukas/utils.rb new file mode 100644 index 0000000..2100659 --- /dev/null +++ b/lib/sheet_zoukas/utils.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +module SheetZoukas + # Utility methods + class Utils + def self.vars_present?(required_vars, message) + all_present = true + required_vars.each do |var| + if ENV.fetch(var, '').strip.empty? + all_present = false + puts "⛔️ #{var} #{message}." + end + end + all_present + end + end +end diff --git a/lib/sheet_zoukas/version.rb b/lib/sheet_zoukas/version.rb index 9d22d16..9e723b3 100644 --- a/lib/sheet_zoukas/version.rb +++ b/lib/sheet_zoukas/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module SheetZoukas - VERSION = '0.1.0' + VERSION = '0.2.0' end diff --git a/spec/sheet_zoukas/utils_spec.rb b/spec/sheet_zoukas/utils_spec.rb new file mode 100644 index 0000000..307a886 --- /dev/null +++ b/spec/sheet_zoukas/utils_spec.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +# rubocop:disable RSpec/MultipleExpectations + +require 'spec_helper' +require 'sheet_zoukas/utils' + +RSpec.describe SheetZoukas::Utils do + describe '.vars_present?' do + before { ENV.store('PRESENT_VAR', 'present') } + + it 'returns false when missing vars' do + expect do + expect(described_class).not_to be_vars_present(%w[MISSING_VAR PRESENT_VAR], 'is missing') + end.to output("⛔️ MISSING_VAR is missing.\n").to_stdout + end + + it 'returns true when no missing vars' do + expect do + expect(described_class).to be_vars_present(%w[PRESENT_VAR], 'is missing') + end.not_to output.to_stdout + end + + it 'returns false when variable empty string' do + ENV.store('EMPTY_VAR', '') + + expect do + expect(described_class).not_to be_vars_present(%w[PRESENT_VAR EMPTY_VAR], 'is missing') + end.to output("⛔️ EMPTY_VAR is missing.\n").to_stdout + end + + it 'returns false when variable blank string' do + ENV.store('EMPTY_VAR', ' ') + + expect do + expect(described_class).not_to be_vars_present(%w[PRESENT_VAR EMPTY_VAR], 'is missing') + end.to output("⛔️ EMPTY_VAR is missing.\n").to_stdout + end + end +end + +# rubocop:enable RSpec/MultipleExpectations diff --git a/spec/sheet_zoukas_spec.rb b/spec/sheet_zoukas_spec.rb index 77dfed8..0cd6725 100644 --- a/spec/sheet_zoukas_spec.rb +++ b/spec/sheet_zoukas_spec.rb @@ -20,5 +20,17 @@ 'Reward Type' => 'no longer active') end end + + it 'fails when missing required environment variable' do # rubocop:disable RSpec/ExampleLength, RSpec/MultipleExpectations + missing = SheetZoukas::REQUIRED_VARS.first + ENV.delete(missing) + expect do + expect do + described_class.retrieve_sheet_json(ENV.fetch('GOOGLE_API_SPREADSHEET_ID', nil), 'Log') + end.to raise_error(SystemExit) do |error| # rubocop:disable Style/MultilineBlockChain + expect(error.status).to eq(1) + end + end.to output("⛔️ #{missing} required for Google Sheets API calls.\n").to_stdout + end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 9b4ca16..1d79510 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -30,20 +30,7 @@ config.filter_sensitive_data('') { ENV.fetch('GOOGLE_API_SPREADSHEET_ID', nil) } end -REQUIRED_VARS = %w[GOOGLE_ACCOUNT_TYPE GOOGLE_API_KEY GOOGLE_CLIENT_EMAIL GOOGLE_CLIENT_ID GOOGLE_PRIVATE_KEY - GOOGLE_API_SPREADSHEET_ID].freeze - -def check_vars - err = false - REQUIRED_VARS.each do |var| - if ENV.fetch(var, nil)&.chars - puts "✅ #{var}" - else - err = true - puts "⛔️ #{var} required for tests to run." - end - end - exit 1 if err -end - -check_vars +# make sure all vars required for testing are present +require 'sheet_zoukas/utils' +REQUIRED_VARS_TEST = (SheetZoukas::REQUIRED_VARS + ['GOOGLE_API_SPREADSHEET_ID']).freeze +exit 1 unless SheetZoukas::Utils.vars_present?(REQUIRED_VARS_TEST, 'required for tests to run')