Refactor CLI#run! method
This commit is contained in:
		@@ -11,8 +11,15 @@ module Producer
 | 
				
			|||||||
      end
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      def run!
 | 
					      def run!
 | 
				
			||||||
        print_usage_and_exit(64) unless @arguments.length == 2
 | 
					        check_arguments!
 | 
				
			||||||
 | 
					        evaluate_recipe_file(@arguments[1])
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      def check_arguments!
 | 
				
			||||||
 | 
					        print_usage_and_exit(64) unless @arguments.length == 2
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      def evaluate_recipe_file(filepath)
 | 
				
			||||||
        recipe = Recipe.from_file(@arguments[1])
 | 
					        recipe = Recipe.from_file(@arguments[1])
 | 
				
			||||||
        env = Env.new(recipe)
 | 
					        env = Env.new(recipe)
 | 
				
			||||||
        recipe.evaluate env
 | 
					        recipe.evaluate env
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -13,18 +13,51 @@ module Producer::Core
 | 
				
			|||||||
      end
 | 
					      end
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # FIXME: spec/method need refactoring
 | 
					 | 
				
			||||||
    describe '#run!' do
 | 
					    describe '#run!' do
 | 
				
			||||||
 | 
					      it 'checks the arguments' do
 | 
				
			||||||
 | 
					        expect(cli).to receive(:check_arguments!)
 | 
				
			||||||
 | 
					        cli.run!
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      it 'evaluates the recipe' do
 | 
				
			||||||
 | 
					        expect(cli).to receive(:evaluate_recipe_file).with(arguments[1])
 | 
				
			||||||
 | 
					        cli.run!
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    describe '#check_arguments!' do
 | 
				
			||||||
 | 
					      context 'when an argument is missing' do
 | 
				
			||||||
 | 
					        let(:arguments) { %w{host} }
 | 
				
			||||||
 | 
					        let(:stdout)    { StringIO.new }
 | 
				
			||||||
 | 
					        subject(:cli)   { CLI.new(arguments, stdout) }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        it 'exits with a return status of 64' do
 | 
				
			||||||
 | 
					          expect { cli.check_arguments! }.to raise_error(SystemExit) { |e|
 | 
				
			||||||
 | 
					            expect(e.status).to eq 64
 | 
				
			||||||
 | 
					          }
 | 
				
			||||||
 | 
					        end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        it 'prints the usage' do
 | 
				
			||||||
 | 
					          begin
 | 
				
			||||||
 | 
					            cli.check_arguments!
 | 
				
			||||||
 | 
					          rescue SystemExit
 | 
				
			||||||
 | 
					          end
 | 
				
			||||||
 | 
					          expect(stdout.string).to match /\AUsage: .+/
 | 
				
			||||||
 | 
					        end
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    describe '#evaluate_recipe_file' do
 | 
				
			||||||
      it 'builds a recipe' do
 | 
					      it 'builds a recipe' do
 | 
				
			||||||
        expect(Recipe).to receive(:from_file).with(arguments[1]).and_call_original
 | 
					        expect(Recipe).to receive(:from_file).with(arguments[1]).and_call_original
 | 
				
			||||||
        cli.run!
 | 
					        cli.evaluate_recipe_file(arguments[1])
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      it 'builds an environment with the current recipe' do
 | 
					      it 'builds an environment with the current recipe' do
 | 
				
			||||||
        recipe = double('recipe').as_null_object
 | 
					        recipe = double('recipe').as_null_object
 | 
				
			||||||
        allow(Recipe).to receive(:from_file).and_return(recipe)
 | 
					        allow(Recipe).to receive(:from_file).and_return(recipe)
 | 
				
			||||||
        expect(Env).to receive(:new).with(recipe).and_call_original
 | 
					        expect(Env).to receive(:new).with(recipe).and_call_original
 | 
				
			||||||
        cli.run!
 | 
					        cli.evaluate_recipe_file(arguments[1])
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      it 'evaluates the recipe with the environment' do
 | 
					      it 'evaluates the recipe with the environment' do
 | 
				
			||||||
@@ -33,27 +66,7 @@ module Producer::Core
 | 
				
			|||||||
        env = double('env')
 | 
					        env = double('env')
 | 
				
			||||||
        allow(Env).to receive(:new).and_return(env)
 | 
					        allow(Env).to receive(:new).and_return(env)
 | 
				
			||||||
        expect(recipe).to receive(:evaluate).with(env)
 | 
					        expect(recipe).to receive(:evaluate).with(env)
 | 
				
			||||||
        cli.run!
 | 
					        cli.evaluate_recipe_file(arguments[1])
 | 
				
			||||||
      end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      context 'missing argument' do
 | 
					 | 
				
			||||||
        let(:arguments) { %w{host} }
 | 
					 | 
				
			||||||
        let(:stdout)    { StringIO.new }
 | 
					 | 
				
			||||||
        subject(:cli)   { CLI.new(arguments, stdout) }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        it 'exits' do
 | 
					 | 
				
			||||||
          expect { cli.run! }.to raise_error(SystemExit) { |e|
 | 
					 | 
				
			||||||
            expect(e.status).to eq 64
 | 
					 | 
				
			||||||
          }
 | 
					 | 
				
			||||||
        end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        it 'prints the usage' do
 | 
					 | 
				
			||||||
          begin
 | 
					 | 
				
			||||||
            cli.run!
 | 
					 | 
				
			||||||
          rescue SystemExit
 | 
					 | 
				
			||||||
          end
 | 
					 | 
				
			||||||
          expect(stdout.string).to match /\AUsage: .+/
 | 
					 | 
				
			||||||
        end
 | 
					 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user