spec/localeapp/cli/install_spec.rb in localeapp-2.1.1 vs spec/localeapp/cli/install_spec.rb in localeapp-2.2.0

- old
+ new

@@ -1,65 +1,80 @@ require 'spec_helper' require 'localeapp/cli/install' -describe Localeapp::CLI::Install, '.execute(key = nil)' do - let(:output) { StringIO.new } - let(:key) { 'MYAPIKEY' } - let(:command) { Localeapp::CLI::Install.new(:output => output) } +describe Localeapp::CLI::Install, "#execute" do + let(:key) { "MYAPIKEY" } + let(:installer) { double "installer" } + subject(:command) { described_class.new output: output } - it "creates the installer based on the config type" do + it "executes the appropriate installer based on the config type" do command.config_type = :heroku - expect(command).to receive(:installer).with("HerokuInstaller").and_return(double.as_null_object) - command.execute(key) + allow(Localeapp::CLI::Install::HerokuInstaller).to receive :new do + installer + end + expect(installer).to receive :execute + command.execute key end it "executes the installer with the given key" do - installer = double(:installer) - expect(installer).to receive(:execute).with(key) - allow(command).to receive(:installer).and_return(installer) - command.execute(key) + allow(Localeapp::CLI::Install::DefaultInstaller).to receive :new do + installer + end + expect(installer) + .to receive(:execute) + .with key, anything + command.execute key end + + it "executes the installer with the given options" do + allow(Localeapp::CLI::Install::DefaultInstaller).to receive :new do + installer + end + expect(installer) + .to receive(:execute) + .with anything, foo: :bar + command.execute key, foo: :bar + end end -describe Localeapp::CLI::Install::DefaultInstaller, '#execute(key = nil)' do - let(:output) { StringIO.new } - let(:key) { 'MYAPIKEY' } - let(:installer) { Localeapp::CLI::Install::DefaultInstaller.new(output) } +describe Localeapp::CLI::Install::DefaultInstaller, "#execute" do + let(:key) { "MYAPIKEY" } + subject(:installer) { described_class.new StringIO.new } - before do - allow(installer).to receive(:print_header) - allow(installer).to receive(:validate_key).and_return(false) - end + context "when key validation fails" do + before do + allow(installer).to receive(:print_header) + allow(installer).to receive(:validate_key).and_return(false) + end - it "prints the header" do - expect(installer).to receive(:print_header) - installer.execute - end + it "prints the header" do + expect(installer).to receive(:print_header) + installer.execute + end - it "validates the key" do - expect(installer).to receive(:key=).with(key) - expect(installer).to receive(:validate_key) - installer.execute(key) - end + it "validates the key" do + expect(installer).to receive(:key=).with(key) + expect(installer).to receive(:validate_key) + installer.execute(key) + end - context "When key validation fails" do it "returns false" do expect(installer.execute(key)).to eq(false) end end - context "When key validation is successful" do + context "when key validation is successful" do before do allow(installer).to receive(:validate_key).and_return(true) - allow(installer).to receive(:check_default_locale) + allow(installer).to receive(:print_default_locale) allow(installer).to receive(:set_config_paths) allow(installer).to receive(:write_config_file) allow(installer).to receive(:check_data_directory_exists) end it "checks the default locale" do - expect(installer).to receive(:check_default_locale) + expect(installer).to receive(:print_default_locale) installer.execute(key) end it "sets the configuration paths" do expect(installer).to receive(:set_config_paths) @@ -78,10 +93,36 @@ it "returns true" do expect(installer.execute(key)).to eq(true) end end + + context "when given `write_env_file' option with a path" do + let :key_checker do + double "key checker", check: [true, Hash.new({})] + end + subject :installer do + described_class.new StringIO.new, key_checker: key_checker + end + + around do |example| + Dir.mktmpdir("localeapp-spec") { |dir| Dir.chdir(dir) { example.run } } + end + + it "writes the API key and a new line to the file at given path" do + installer.execute key, write_env_file: "some_env_file" + expect(File.read("some_env_file")) + .to eq "LOCALEAPP_API_KEY=#{key}\n" + end + + it "appends the API key at the end of the file" do + File.open("some_env_file", "w") { |f| f.puts "FOO=BAR" } + installer.execute key, write_env_file: "some_env_file" + expect(File.read("some_env_file")) + .to match /\AFOO=BAR\nLOCALEAPP_API_KEY/ + end + end end describe Localeapp::CLI::Install::DefaultInstaller, '#validate_key(key)' do let(:output) { StringIO.new } let(:key) { 'MYAPIKEY' } @@ -108,28 +149,26 @@ installer.validate_key expect(output.string).to match(/Test Project/) end end -describe Localeapp::CLI::Install::DefaultInstaller, '#check_default_locale' do +describe Localeapp::CLI::Install::DefaultInstaller, '#print_default_locale' do let(:output) { StringIO.new } let(:installer) { Localeapp::CLI::Install::DefaultInstaller.new(output) } before do allow(installer).to receive(:project_data).and_return(valid_project_data) end it "displays project base locale" do - installer.check_default_locale + installer.print_default_locale expect(output.string).to match(/en \(English\)/) end - it "displays warning if I18n.default_locale doesn't match what's configured on localeapp.com" do - allow(I18n).to receive(:default_locale).and_return(:es) - installer.check_default_locale - expect(output.string) - .to match(%r{WARNING: I18n\.default_locale is es, change in config/application\.rb \(Rails 3\+\)}) + it "warns that I18n.default_locale must match project locale" do + installer.print_default_locale + expect(output.string).to include "Please ensure I18n.default_locale is en" end end describe Localeapp::CLI::Install::DefaultInstaller, '#set_config_paths' do let(:output) { StringIO.new } @@ -146,28 +185,29 @@ it "sets the data directory for a rails app" do expect(installer.data_directory).to eq("config/locales") end end -describe Localeapp::CLI::Install::DefaultInstaller, '#write_config_file' do - let(:output) { StringIO.new } - let(:config_file_path) { 'config/initializers/localeapp.rb' } - let(:key) { 'APIKEY' } - let(:installer) { Localeapp::CLI::Install::DefaultInstaller.new(output) } +describe Localeapp::CLI::Install::DefaultInstaller, "#write_config_file" do + let(:config_file_path) { "config/initializers/localeapp.rb" } + let(:key) { "APIKEY" } + let(:file) { double "file" } + subject(:installer) { described_class.new StringIO.new } - it "creates a configuration file containing just the api key" do - installer.key = key + before do + allow(File).to receive(:open).and_yield file installer.config_file_path = config_file_path - file = double('file') + end + + it "creates a configuration file reading the API key from the environment" do expect(file).to receive(:write).with <<-CONTENT require 'localeapp/rails' Localeapp.configure do |config| - config.api_key = 'APIKEY' + config.api_key = ENV['LOCALEAPP_API_KEY'] end CONTENT - expect(File).to receive(:open).with(config_file_path, 'w+').and_yield(file) installer.write_config_file end end describe Localeapp::CLI::Install::DefaultInstaller, '#check_data_directory_exists' do @@ -220,16 +260,16 @@ CONTENT expect(File).to receive(:open).with(config_file_path, 'w+').and_yield(file) installer.write_config_file end end -describe Localeapp::CLI::Install::StandaloneInstaller, '#check_default_locale' do +describe Localeapp::CLI::Install::StandaloneInstaller, '#print_default_locale' do let(:output) { StringIO.new } let(:installer) { Localeapp::CLI::Install::StandaloneInstaller.new(output) } it "does nothing" do - installer.check_default_locale + installer.print_default_locale expect(output.string).to eq('') end end describe Localeapp::CLI::Install::StandaloneInstaller, '#set_config_paths' do @@ -247,31 +287,31 @@ it "sets the data directory for a standalone app" do expect(installer.data_directory).to eq("locales") end end -describe Localeapp::CLI::Install::StandaloneInstaller, '#write_config_file' do - let(:output) { StringIO.new } - let(:key) { 'APIKEY' } - let(:config_file_path) { '.localeapp/config.rb' } - let(:data_directory) { 'locales' } - let(:installer) { Localeapp::CLI::Install::StandaloneInstaller.new(output) } +describe Localeapp::CLI::Install::StandaloneInstaller, "#write_config_file" do + let(:key) { "APIKEY" } + let(:config_file_path) { ".localeapp/config.rb" } + let(:data_directory) { "locales" } + let(:file) { double "file" } + subject(:installer) { described_class.new StringIO.new } - it "creates a configuration file containing the dot file configuration at the given config_file_path" do - allow(installer).to receive(:create_config_dir).and_return(File.dirname(config_file_path)) - installer.key = key + before do + allow(File).to receive(:open).and_yield file installer.config_file_path = config_file_path installer.data_directory = data_directory - file = double('file') + end + + it "creates a configuration file using given config_file_path" do expect(file).to receive(:write).with <<-CONTENT Localeapp.configure do |config| - config.api_key = 'APIKEY' + config.api_key = ENV['LOCALEAPP_API_KEY'] config.translation_data_directory = 'locales' config.synchronization_data_file = '.localeapp/log.yml' config.daemon_pid_file = '.localeapp/localeapp.pid' end CONTENT - expect(File).to receive(:open).with(config_file_path, 'w+').and_yield(file) installer.write_config_file end end describe Localeapp::CLI::Install::GithubInstaller, '#write_config_file' do