spec/unit/generators/base_generator_spec.rb in howitzer-1.1.1 vs spec/unit/generators/base_generator_spec.rb in howitzer-2.0.0

- old
+ new

@@ -29,11 +29,12 @@ end describe 'constructor' do let(:list1) { double(:list1) } let(:list2) { double(:list2) } - subject { described_class.new } + let(:options) { { r: true, rspec: true } } + subject { described_class.new(options) } before do expect_any_instance_of(described_class).to receive(:print_banner).with(no_args).once allow_any_instance_of(described_class).to receive(:manifest) do { files: list1, @@ -43,28 +44,28 @@ end end it do expect_any_instance_of(described_class).to receive(:copy_files).with(list1).once expect_any_instance_of(described_class).to receive(:copy_templates).with(list2).once - subject + expect(subject.instance_variable_get(:@options)).to eql options end end describe '#manifest' do - subject { described_class.new.manifest } + subject { described_class.new({}).manifest } before { allow_any_instance_of(described_class).to receive(:initialize) { nil } } it { is_expected.to be_nil } end describe '#banner' do - subject { described_class.new.send(:banner) } + subject { described_class.new({}).send(:banner) } before { allow_any_instance_of(described_class).to receive(:initialize) { nil } } it { is_expected.to be_nil } end describe '#logger' do - subject { described_class.new.send(:logger) } + subject { described_class.new({}).send(:logger) } before { allow_any_instance_of(described_class).to receive(:initialize) { nil } } context 'when not specified' do before { described_class.instance_variable_set('@logger', nil) } it { is_expected.to eq($stdout) } end @@ -74,46 +75,78 @@ it { is_expected.to eq(output) } end end describe '#destination' do - subject { described_class.new.send(:destination) } + subject { described_class.new({}).send(:destination) } before do allow_any_instance_of(described_class).to receive(:initialize) { nil } allow(described_class).to receive(:destination) { '/' } end it { is_expected.to eq('/') } end describe '#copy_files' do - let(:list) { [ {source: 'example.txt'} ] } + let(:list) { [{ source: 'example.txt' }] } let(:source_path) { '/example_path/example.txt' } - let(:generator) { described_class.new } + let(:generator) { described_class.new({}) } subject { generator.send(:copy_files, list) } before do allow_any_instance_of(described_class).to receive(:initialize) { nil } allow(generator).to receive(:source_path).with(list.first[:source]) { source_path } end after { subject } context 'when source_file exists' do - before { allow(File).to receive(:exists?).with(source_path) { true } } + before { allow(File).to receive(:exist?).with(source_path) { true } } it { expect(generator).to receive(:copy_with_path).with(list.first).once } end context 'when source_file missing' do - before { allow(File).to receive(:exists?).with(source_path) { false } } + before { allow(File).to receive(:exist?).with(source_path) { false } } it { expect(generator).to receive(:puts_error).with("File '/example_path/example.txt' was not found.").once } end end describe '#copy_templates' do - subject { described_class.new.send(:copy_templates, nil) } - before { allow_any_instance_of(described_class).to receive(:initialize) { nil } } - it { is_expected.to be_nil } + let(:list) { [{ source: 'example.txt.erb', destination: 'example.txt' }] } + let(:source_path) { '/example_path/example.txt.erb' } + let(:destination_path) { '/example_path/example.txt' } + let(:generator) { described_class.new('rspec' => true) } + subject { generator.send(:copy_templates, list) } + before do + allow_any_instance_of(described_class).to receive(:initialize) { nil } + allow(generator).to receive(:source_path).with(list.first[:source]) { source_path } + allow(generator).to receive(:dest_path).with(list.first[:destination]) { destination_path } + allow(generator).to receive(:write_template).with(destination_path, source_path) + allow(generator).to receive(:gets) { 'h' } + end + after { subject } + context 'when destination file exists' do + before { allow(File).to receive(:exist?).with(destination_path) { true } } + it { expect(generator).to receive(:puts_info).with("Conflict with '#{list.first[:destination]}' template").once } + it do + expect(generator).to receive(:print_info).with( + " Overwrite '#{list.first[:destination]}' template? [Yn]:" + ).once + end + context 'and answer is yes' do + before { allow(generator).to receive(:gets) { 'y' } } + it { expect(generator).to receive(:write_template).with(destination_path, source_path).once } + it { expect(generator).to receive(:puts_info).twice } + end + context 'and answer is no' do + before { allow(generator).to receive(:gets) { 'n' } } + it { expect(generator).to receive(:puts_info).twice } + end + end + context 'when source file exists' do + before { allow(File).to receive(:exist?).with(destination_path) { false } } + it { expect(generator).to receive(:write_template).with(destination_path, source_path).once } + end end describe '#print_banner' do - let(:generator) { described_class.new } + let(:generator) { described_class.new({}) } subject { generator.send(:print_banner) } before do allow_any_instance_of(described_class).to receive(:initialize) { nil } allow(generator).to receive(:banner) { banner } end @@ -127,48 +160,47 @@ it { expect(described_class.logger).not_to receive(:puts) } end end describe '#print_info' do - subject { described_class.new.send(:print_info, 'data') } + subject { described_class.new({}).send(:print_info, 'data') } before { allow_any_instance_of(described_class).to receive(:initialize) { nil } } after { subject } - it { expect(described_class.logger).to receive(:print).with(' data')} + it { expect(described_class.logger).to receive(:print).with(' data') } end describe '#puts_info' do - subject { described_class.new.send(:puts_info, 'data') } + subject { described_class.new({}).send(:puts_info, 'data') } before { allow_any_instance_of(described_class).to receive(:initialize) { nil } } after { subject } - it { expect(described_class.logger).to receive(:puts).with(' data')} + it { expect(described_class.logger).to receive(:puts).with(' data') } end describe '#puts_error' do - subject { described_class.new.send(:puts_error, 'data') } + subject { described_class.new({}).send(:puts_error, 'data') } before { allow_any_instance_of(described_class).to receive(:initialize) { nil } } after { subject } - it { expect(described_class.logger).to receive(:puts).with(' ERROR: data')} + it { expect(described_class.logger).to receive(:puts).with(' ERROR: data') } end describe '#source_path' do - subject { described_class.new.send(:source_path, 'example.txt') } + subject { described_class.new({}).send(:source_path, 'example.txt') } before do allow_any_instance_of(described_class).to receive(:initialize) { nil } - allow(File).to receive(:dirname) { '/' } end - it { is_expected.to eq('/base/templates/example.txt') } + it { is_expected.to include('/base/templates/example.txt') } end describe '#dest_path' do - subject { described_class.new.send(:dest_path, 'example.txt') } + subject { described_class.new({}).send(:dest_path, 'example.txt') } before { allow_any_instance_of(described_class).to receive(:initialize) { nil } } it { is_expected.to include('/example.txt') } end describe '#copy_with_path' do - let(:generator) { described_class.new } - let(:data) { {source: 's.txt', destination: 'd.txt'} } + let(:generator) { described_class.new({}) } + let(:data) { { source: 's.txt', destination: 'd.txt' } } let(:src) { '/path/to/s.txt' } let(:dst) { '/path/to/d.txt' } subject { generator.send(:copy_with_path, data) } before do allow_any_instance_of(described_class).to receive(:initialize) { nil } @@ -176,11 +208,11 @@ allow(generator).to receive(:dest_path).with('d.txt') { dst } allow(FileUtils).to receive(:mkdir_p).with('/path/to') { true } end after { subject } context 'when destination file present' do - before { allow(File).to receive(:exists?).with(dst) { true } } + before { allow(File).to receive(:exist?).with(dst) { true } } context 'when identical with source file' do before { allow(FileUtils).to receive(:identical?).with(src, dst) { true } } it { expect(generator).to receive(:puts_info).with("Identical 'd.txt' file").once } end context 'when not identical with source file' do @@ -190,18 +222,18 @@ expect(generator).to receive(:print_info).with(" Overwrite 'd.txt' file? [Yn]:") end context 'when user typed Y' do before { allow(generator).to receive(:gets) { 'Y' } } it do - expect(FileUtils).to receive(:cp).with(src, dst) {nil}.once + expect(FileUtils).to receive(:cp).with(src, dst) { nil }.once expect(generator).to receive(:puts_info).with(" Forced 'd.txt' file") end end context 'when user typed y' do before { allow(generator).to receive(:gets) { 'y' } } it do - expect(FileUtils).to receive(:cp).with(src, dst) {nil}.once + expect(FileUtils).to receive(:cp).with(src, dst) { nil }.once expect(generator).to receive(:puts_info).with(" Forced 'd.txt' file") end end context 'when user typed N' do before { allow(generator).to receive(:gets) { 'N' } } @@ -225,18 +257,17 @@ end end end end context 'when destination file missing' do - before { allow(File).to receive(:exists?).with(dst) { false } } + before { allow(File).to receive(:exist?).with(dst) { false } } it do expect(generator).to receive(:puts_info).with("Added 'd.txt' file") expect(FileUtils).to receive(:cp).with(src, dst).once end end context 'when exception happened' do before { allow(FileUtils).to receive(:mkdir_p).and_raise(StandardError.new('Some error')) } - it { expect(generator).to receive(:puts_error).with("Impossible to create 'd.txt' file. Reason: Some error")} + it { expect(generator).to receive(:puts_error).with("Impossible to create 'd.txt' file. Reason: Some error") } end end - -end \ No newline at end of file +end