spec/handbrake/cli_spec.rb in handbrake-0.1.0 vs spec/handbrake/cli_spec.rb in handbrake-0.2.0

- old
+ new

@@ -69,17 +69,170 @@ pending 'HandBrakeCLI is not on the path' end end it 'works' do - lambda { cli.update }.should_not raise_error + lambda { cli.preset_list }.should_not raise_error end end describe '#output' do + let(:filename) { File.join(tmpdir, 'foo.m4v') } + + def it_should_have_run(expected_fn=filename) + runner.actual_arguments.should == ['--output', expected_fn] + end + + def it_should_not_have_run + runner.actual_arguments.should be_nil + end + it 'uses the --output argument' do - cli.output('/foo/bar.m4v') - runner.actual_arguments.should == %w(--output /foo/bar.m4v) + cli.output(filename) + runner.actual_arguments.should == ['--output', filename] + end + + it 'fails for an unknown option' do + lambda { cli.output(filename, :quux => 'zap') }. + should raise_error('Unknown options for output: [:quux]') + end + + describe ':overwrite' do + context 'true' do + it 'executes when the file does not exist' do + cli.output(filename, :overwrite => true) + it_should_have_run + end + + it 'executes when the file exists' do + FileUtils.touch filename + cli.output(filename, :overwrite => true) + it_should_have_run + end + + it 'is the default' do + FileUtils.touch filename + cli.output(filename) + it_should_have_run + end + end + + context 'false' do + it 'executes when the file does not exist' do + cli.output(filename, :overwrite => false) + it_should_have_run + end + + it 'throws an exception when the file does exist' do + FileUtils.touch filename + lambda { cli.output(filename, :overwrite => false) }. + should raise_error(HandBrake::FileExistsError) + end + end + + context ':ignore' do + it 'executes when the file does not exist' do + cli.output(filename, :overwrite => :ignore) + it_should_have_run + end + + it 'does nothing when the file does exist' do + FileUtils.touch filename + cli.output(filename, :overwrite => :ignore) + it_should_not_have_run + end + end + + context 'other value' do + it 'throws an exception' do + lambda { cli.output(filename, :overwrite => 'flip') }. + should raise_error('Unsupported value for :overwrite: "flip"') + end + end + end + + describe ':atomic' do + context 'false' do + it 'executes normally' do + cli.output(filename, :atomic => false) + it_should_have_run + end + + it 'is the default' do + cli.output(filename) + it_should_have_run + end + end + + context 'true' do + let(:expected_temporary_file) { File.join(tmpdir, "foo.handbraking.m4v") } + + it 'outputs to the temporary file with ".handbraking" inserted before the extension' do + cli.output(filename, :atomic => true) + it_should_have_run(expected_temporary_file) + end + + it 'appends .handbraking if the output file does not have an extension' do + cli.output(File.join(tmpdir('a.b.c'), 'foo'), :atomic => true) + it_should_have_run(File.join(tmpdir('a.b.c'), 'foo.handbraking')) + end + + it 'copies the output to the desired filename' do + cli.output(filename, :atomic => true) + File.read(filename).should == 'This is the file created by --output' + end + + it 'removes the temporary file' do + cli.output(filename, :atomic => true) + File.exist?(expected_temporary_file).should be_false + end + + it 'overwrites the temporary file if it exists' do + FileUtils.touch expected_temporary_file + cli.output(filename, :atomic => true) + it_should_have_run(expected_temporary_file) + end + + describe 'and :overwrite' do + describe 'false' do + it 'throws an exception if the target filename exists initially' do + FileUtils.touch filename + lambda { cli.output(filename, :atomic => true, :overwrite => false) }. + should raise_error(HandBrake::FileExistsError) + end + + it 'throws an exception if the target filename exists after output' do + runner.behavior = lambda { FileUtils.touch filename } + lambda { cli.output(filename, :atomic => true, :overwrite => false) }. + should raise_error(HandBrake::FileExistsError) + end + end + + describe ':ignore' do + it 'does nothing if the target filename exists initially' do + FileUtils.touch filename + cli.output(filename, :atomic => true, :overwrite => :ignore) + it_should_not_have_run + end + + it 'does not copy the output if the target filename exists after encoding' do + runner.behavior = lambda { + File.open(filename, 'w') { |f| f.write 'Other process result' } + } + cli.output(filename, :atomic => true, :overwrite => :ignore) + File.read(filename).should == 'Other process result' + end + + it 'does not remove the temporary output if the target filename exists after encoding' do + runner.behavior = lambda { + File.open(filename, 'w') { |f| f.write 'Other process result' } + } + cli.output(filename, :atomic => true, :overwrite => :ignore) + File.exist?(expected_temporary_file).should be_true + end + end + end + end end end describe '#scan' do let(:sample_scan) { File.read(File.expand_path('../sample-titles-scan.err', __FILE__)) }