view spec/lib/pmuxinator/cli_spec.rb @ 2:67a6071afec7

Rename tmuxinator -> pmuxinator $ zmv **/*tmuxinator* **/*pmuxinator* $ gsed -e 's/tmuxinator/pmuxinator/g' -i **/*.* $ gsed -e 's/Tmuxinator/Pmuxinator/g' -i **/*.*
author Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
date Mon, 21 Jul 2014 08:32:00 +0900
parents
children
line wrap: on
line source

require "spec_helper"
describe Pmuxinator::Cli do
  let(:cli) { Pmuxinator::Cli }

  before do
    ARGV.clear
    allow(Kernel).to receive(:system)
    allow(FileUtils).to receive(:copy_file)
    allow(FileUtils).to receive(:rm)
    allow(FileUtils).to receive(:remove_dir)
  end

  context "no arguments" do
    it "runs without error" do
      _, err = capture_io { cli.start }
      expect(err).to be_empty
    end
  end

  describe "#completions" do
    before do
      ARGV.replace(["completions", "start"])
      allow(Pmuxinator::Config).to receive_messages(:configs => ["test.yml"])
    end

    it "gets completions" do
      out, _ = capture_io { cli.start }
      expect(out).to include("test.yml")
    end
  end

  describe "#commands" do
    before do
      ARGV.replace(["commands"])
    end

    it "lists the commands" do
      out, _ = capture_io { cli.start }
      expect(out).to eq "#{%w(commands copy debug delete doctor help implode list start version).join("\n")}\n"
    end
  end

  describe "#start" do
    before do
      ARGV.replace(["start", "foo"])
      allow(Pmuxinator::Config).to receive_messages(:validate => project)
      allow(Pmuxinator::Config).to receive_messages(:version => 1.9)
      allow(Kernel).to receive(:exec)
    end

    context "no deprecations" do
      let(:project) { FactoryGirl.build(:project) }

      it "starts the project" do
        expect(Kernel).to receive(:exec)
        capture_io { cli.start }
      end
    end

    context "deprecations" do
      before do
        allow($stdin).to receive_messages(:getc => "y")
      end

      let(:project) { FactoryGirl.build(:project_with_deprecations) }

      it "prints the deprecations" do
        out, _ = capture_io { cli.start }
        expect(out).to include "DEPRECATION"
      end
    end
  end

  describe "#new" do
    let(:file) { StringIO.new }

    before do
      ARGV.replace(["new", "test"])
      allow(File).to receive(:open) { |&block| block.yield file }
    end

    context "existing project doesn't exist" do
      before do
        allow(Pmuxinator::Config).to receive_messages(:exists? => false)
      end

      it "creates a new pmuxinator project file" do
        capture_io { cli.start }
        expect(file.string).to_not be_empty
      end
    end

    context "files exists" do
      before do
        allow(File).to receive_messages(:exists? => true)
      end

      it "just opens the file" do
        expect(Kernel).to receive(:system)
        capture_io { cli.start }
      end
    end
  end

  describe "#copy" do
    before do
      ARGV.replace(["copy", "foo", "bar"])
      allow(Pmuxinator::Config).to receive(:exists?) { true }
    end

    context "new project already exists" do
      before do
        allow(Thor::LineEditor).to receive_messages(:readline => "y")
      end

      it "prompts user to confirm overwrite" do
        expect(FileUtils).to receive(:rm)
        capture_io { cli.start }
      end

      it "copies the config" do
        expect(FileUtils).to receive(:copy_file)
        capture_io { cli.start }
      end
    end

    context "existing project doens't exist" do
      before do
        allow(Pmuxinator::Config).to receive(:exists?) { false }
      end

      it "exit with error code" do
        expect { capture_io { cli.start } }.to raise_error SystemExit
      end
    end
  end

  describe "#debug" do
    let(:project) { FactoryGirl.build(:project) }

    before do
      ARGV.replace(["debug", "foo"])
      allow(Pmuxinator::Config).to receive_messages(:validate => project)
    end

    it "renders the project" do
      expect(project).to receive(:render)
      capture_io { cli.start }
    end
  end

  describe "#delete" do
    before do
      ARGV.replace(["delete", "foo"])
      allow(Thor::LineEditor).to receive_messages(:readline => "y")
    end

    context "project exists" do
      before do
        allow(Pmuxinator::Config).to receive(:exists?) { true }
      end

      it "deletes the project" do
        expect(FileUtils).to receive(:rm)
        capture_io { cli.start }
      end
    end

    context "project doesn't exist" do
      before do
        allow(Thor::LineEditor).to receive_messages(:readline => "y")
      end

      it "exits with error message" do
        expect { capture_io { cli.start } }.to raise_error SystemExit
      end
    end
  end

  describe "#implode" do
    before do
      ARGV.replace(["implode"])
      allow(Thor::LineEditor).to receive_messages(:readline => "y")
    end

    it "confirms deletion of all projects" do
      expect(Thor::LineEditor).to receive(:readline).and_return("y")
      capture_io { cli.start }
    end

    it "deletes all projects" do
      expect(FileUtils).to receive(:remove_dir)
      capture_io { cli.start }
    end
  end

  describe "#list" do
    before do
      ARGV.replace(["list"])
      allow(Dir).to receive_messages(:[] => ["/path/to/project.yml"])
    end

    it "lists all projects" do
      expect { capture_io { cli.start } }.to_not raise_error
    end
  end

  describe "#version" do
    before do
      ARGV.replace(["version"])
    end

    it "prints the current version" do
      out, _ = capture_io { cli.start }
      expect(out).to eq "pmuxinator #{Pmuxinator::VERSION}\n"
    end
  end

  describe "#doctor" do
    before do
      ARGV.replace(["doctor"])
    end

    it "checks requirements" do
      expect(Pmuxinator::Config).to receive(:installed?)
      expect(Pmuxinator::Config).to receive(:editor?)
      expect(Pmuxinator::Config).to receive(:shell?)
      capture_io { cli.start }
    end
  end
end