Skip to content

Commit

Permalink
Merge pull request #359 from adamstrickland/edit-delete-fix_failing-test
Browse files Browse the repository at this point in the history
Tests for edit-delete fix
  • Loading branch information
adamstrickland committed Nov 30, 2015
2 parents f938953 + ee76342 commit 6feda8e
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 18 deletions.
32 changes: 21 additions & 11 deletions lib/tmuxinator/cli.rb
Expand Up @@ -67,22 +67,32 @@ def completions(arg)
desc: "Create local project file at ./.tmuxinator.yml"

def new(name)
project_file = if options[:local]
Tmuxinator::Config::LOCAL_DEFAULT
else
Tmuxinator::Config.default_project(name)
end
unless Tmuxinator::Config.exists?(project_file)
project_file = find_project_file(name, options[:local])
Kernel.system("$EDITOR #{project_file}") || doctor
end

no_commands do
def find_project_file(name, local = false)
path = if local
Tmuxinator::Config::LOCAL_DEFAULT
else
Tmuxinator::Config.default_project(name)
end
if File.exists?(path)
path
else
generate_project_file(name, path)
end
end

def generate_project_file(name, path)
template = Tmuxinator::Config.default? ? :default : :sample
content = File.read(Tmuxinator::Config.send(template.to_sym))
erb = Erubis::Eruby.new(content).result(binding)
File.open(project_file, "w") { |f| f.write(erb) }
File.open(path, "w") { |f| f.write(erb) }
path
end

Kernel.system("$EDITOR #{project_file}") || doctor
end

no_commands do
def create_project(project_options = {})
attach_opt = project_options[:attach]
attach = !attach_opt.nil? && attach_opt ? true : false
Expand Down
2 changes: 1 addition & 1 deletion lib/tmuxinator/version.rb
@@ -1,3 +1,3 @@
module Tmuxinator
VERSION = "0.7.0"
VERSION = "0.7.1"
end
111 changes: 105 additions & 6 deletions spec/lib/tmuxinator/cli_spec.rb
Expand Up @@ -149,6 +149,41 @@
end
end

describe "#edit" do
let(:file) { StringIO.new }
let(:name) { "test" }
let(:path) { Tmuxinator::Config.default_project(name) }

context "when the project file _does_ already exist" do
let(:extra) { " - extra: echo 'foobar'" }

before do
# make sure that no project file exists initially
FileUtils.remove_file(path) if File.exists?(path)
expect(File).not_to exist(path)

# now generate a project file
expect(Tmuxinator::Cli.new.generate_project_file(name, path)).to eq path
expect(File).to exist path

# add some content to the project file
File.open(path, "w") do |f|
f.write(extra)
f.flush
end
expect(File.read(path)).to match %r{#{extra}}

# get ready to run `tmuxinator edit #{name}`
ARGV.replace ["edit", name]
end

it "should _not_ generate a new project file" do
capture_io { cli.start }
expect(File.read(path)).to match %r{#{extra}}
end
end
end

describe "#new" do
let(:file) { StringIO.new }
let(:name) { "test" }
Expand All @@ -164,7 +199,7 @@

context "existing project doesn't exist" do
before do
expect(Tmuxinator::Config).to receive_messages(exists?: false)
expect(File).to receive_messages(exists?: false)
end

it "creates a new tmuxinator project file" do
Expand All @@ -174,14 +209,15 @@
end

context "files exists" do
let(:command) { "#{ENV['HOME']}\/\.tmuxinator\/#{name}\.yml" }
let(:root_path) { "#{ENV['HOME']}\/\.tmuxinator\/#{name}\.yml" }

before do
expect(Tmuxinator::Config).to receive_messages(exists?: true)
allow(File).to receive(:exists?).with(anything).and_return(false)
expect(File).to receive(:exists?).with(root_path).and_return(true)
end

it "just opens the file" do
expect(Kernel).to receive(:system).with(%r{#{command}})
expect(Kernel).to receive(:system).with(%r{#{root_path}})
capture_io { cli.start }
end
end
Expand All @@ -194,7 +230,7 @@

context "existing project doesn't exist" do
before do
expect(Tmuxinator::Config).to receive(:exists?).at_least(:once) do
allow(File).to receive(:exists?).at_least(:once) do
false
end
end
Expand All @@ -208,7 +244,7 @@
context "files exists" do
let(:path) { Tmuxinator::Config::LOCAL_DEFAULT }
before do
expect(Tmuxinator::Config).to receive(:exists?).with(path) { true }
expect(File).to receive(:exists?).with(path) { true }
end

it "just opens the file" do
Expand Down Expand Up @@ -368,6 +404,69 @@
end
end

describe "#find_project_file" do
let(:name) { "foobar" }
let(:path) { Tmuxinator::Config.default_project(name) }

after(:each) do
FileUtils.remove_file(path) if File.exists?(path)
end

context "when the project file does not already exist" do
before do
expect(File).not_to exist(path), "expected file at #{path} not to exist"
end

it "should generate a project file" do
new_path = Tmuxinator::Cli.new.find_project_file(name, false)
expect(new_path).to eq path
expect(File).to exist new_path
end
end

context "when the project file _does_ already exist" do
let(:extra) { " - extra: echo 'foobar'" }

before do
expect(File).not_to exist(path), "expected file at #{path} not to exist"
expect(Tmuxinator::Cli.new.generate_project_file(name, path)).to eq path
expect(File).to exist path

File.open(path, "w") do |f|
f.write(extra)
f.flush
end
expect(File.read(path)).to match %r{#{extra}}
end

it "should _not_ generate a new project file" do
new_path = Tmuxinator::Cli.new.find_project_file(name, false)
expect(new_path).to eq path
expect(File).to exist new_path
expect(File.read(new_path)).to match %r{#{extra}}
end
end
end

describe "#generate_project_file" do
let(:name) { "foobar" }
let(:path) { Tmuxinator::Config.default_project(name) }

before do
expect(File).not_to exist(path), "expected file at #{path} not to exist"
end

after(:each) do
FileUtils.remove_file(path) if File.exists?(path)
end

it "should always generate a project file" do
new_path = Tmuxinator::Cli.new.generate_project_file(name, path)
expect(new_path).to eq path
expect(File).to exist new_path
end
end

describe "#create_project" do
shared_examples_for :a_proper_project do
it "should create a valid project" do
Expand Down

0 comments on commit 6feda8e

Please sign in to comment.