spec/lib/hackpad/cli/config_spec.rb in hackpad-cli-0.1.0 vs spec/lib/hackpad/cli/config_spec.rb in hackpad-cli-0.1.1

- old
+ new

@@ -4,45 +4,54 @@ require 'hackpad/cli/config' describe Hackpad::Cli::Config do let(:configdir) { File.expand_path('../../../../files', __FILE__) } - let(:configfile) { File.join(configdir, 'default.yml') } + let(:configfile) { File.join(configdir, 'config.yml') } + let(:workspacedir) { File.join(configdir, 'default') } + let(:workspacefile) { File.join(workspacedir, 'config.yml') } let(:options) { { configdir: configdir, workspace: 'default' } } + let(:input) { StringIO.new } + let(:output) { StringIO.new } - before :each do - FileUtils.mkdir_p configdir unless Dir.exist?(configdir) - end + before { FileUtils.mkdir_p configdir unless Dir.exist?(configdir) } + before { FileUtils.mkdir_p workspacedir unless Dir.exist?(workspacedir) } - after :each do - FileUtils.rm configfile if File.exist?(configfile) - end + after { FileUtils.rm configfile if File.exist?(configfile) } + after { FileUtils.rm workspacefile if File.exist?(workspacefile) } - describe '.load' do - let(:config) { { 'xx' => 'oo' } } - - context 'when there is no config file,' do - it 'calls for setup' do - Dir.stub(:exists?).and_return false - subject.stub(:setup).with(configfile, STDIN, STDOUT) - File.open(configfile, 'w') do |f| - f.write YAML.dump(config) - end - expect(subject.load options).to eq config - end + describe '.new' do + context 'when there is already config files created,' do + let(:configvars) { { 'use_colors' => true, 'workspace' => 'default' } } + let(:workspacevars) { { 'client_id' => '123', 'secret' => 'toto', 'site' => 'http://example.com' } } + before { File.open(configfile, 'w') { |f| f.puts YAML.dump(configvars) } } + before { File.open(workspacefile, 'w') { |f| f.puts YAML.dump(workspacevars) } } + let(:config) { Hackpad::Cli::Config.new(options) } + it { + expect(config.secret).to eq 'toto' + expect(config.site).to eq 'http://example.com' + } end - + context 'when there is no config files created,' do + before { input.stub(:gets).and_return('y', 'default', '123', 'toto','http://example.com') } + let(:config) { Hackpad::Cli::Config.new(options, input, output) } + it { + expect(config.secret).to eq 'toto' + expect(config.site).to eq 'http://example.com' + } + end end - describe '.setup' do - context 'when normal input is provided,' do - let(:input) { StringIO.new } - let(:output) { StringIO.new } - it 'handles setup interactively' do - input.stub(:gets).and_return('client_id', 'secret', 'site') - subject.send :setup, configfile, input, output - expect(File.read configfile).to eq "---\nclient_id: client_id\nsecret: secret\nsite: site\n" - end + describe '.patch_1' do + let(:oldconfigfile) { File.join(configdir, 'default.yml') } + let(:oldconfigvars) { { 'client_id' => '123', 'secret' => 'toto', 'site' => 'http://example.com' } } + before { File.open(oldconfigfile, 'w') { |f| f.puts YAML.dump(oldconfigvars) } } + before { input.stub(:gets).and_return('y', 'default') } + after { FileUtils.rm oldconfigfile if File.exist?(oldconfigfile) } + it do + Hackpad::Cli::Config.new(options, input, output) + expect(File.exist? oldconfigfile).to be_false + expect(File.exist? configfile).to be_true end end end