Skip to content

Commit

Permalink
Clean up file specs
Browse files Browse the repository at this point in the history
  • Loading branch information
rzane committed Dec 26, 2019
1 parent 18b5857 commit aae02be
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 319 deletions.
81 changes: 0 additions & 81 deletions lib/geny/rspec.rb

This file was deleted.

67 changes: 0 additions & 67 deletions lib/geny/test.rb

This file was deleted.

33 changes: 13 additions & 20 deletions spec/geny/actions/find_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,50 @@
describe "#replace" do
it "replaces matching files" do
write "a.txt", "hello"
find.replace(tmp.to_s, "hello", "goodbye")
expect(tmp.join("a.txt").read).to eq("goodbye")
find.replace(".", "hello", "goodbye")
expect("a.txt").to have_content("goodbye")
end

it "ignores excluded files" do
write "a.txt", "hello"
write "b.txt", "hello"
find.replace(".", "hello", "goodbye", excluding: /b/)
expect(File.read("a.txt")).to eq("goodbye")
expect(File.read("b.txt")).to eq("hello")
expect("a.txt").to have_content("goodbye")
expect("b.txt").to have_content("hello")
end
end

describe "#replace" do
it "replaces matching filenames" do
write "hello.txt"
find.rename(tmp.to_s, "hello", "goodbye")
expect(entries).to match_array %w[goodbye.txt]
find.rename(".", "hello", "goodbye")
expect(".").to have_entries(%w[goodbye.txt])
end

it "ignores excluded files" do
write "hello.txt"
write "hello-world.txt"
find.rename(tmp.to_s, "hello", "goodbye", excluding: /world/)
expect(entries).to match_array %w[goodbye.txt hello-world.txt]
find.rename(".", "hello", "goodbye", excluding: /world/)
expect(".").to have_entries(%w[goodbye.txt hello-world.txt])
end

it "replaces deeply nested matching filenames" do
write "hello.txt"
write "hello/hello.txt"
write "hello/hello/hello.txt"
find.rename(tmp.to_s, "hello", "goodbye")
find.rename(".", "hello", "goodbye")

expect(entries).to match_array %w[
expect(".").to have_entries(%w[
goodbye.txt
goodbye/goodbye.txt
goodbye/goodbye/goodbye.txt
]
])
end

it "replaces deeply nested matching directories" do
write "hello/hello/foo.txt"
find.rename(tmp.to_s, "hello", "goodbye")
expect(entries).to match_array %w[goodbye/goodbye/foo.txt]
find.rename(".", "hello", "goodbye")
expect(".").to have_entries(%w[goodbye/goodbye/foo.txt])
end
end

def entries
tmp.glob("**/*")
.select(&:file?)
.map { |path| path.relative_path_from(tmp) }
.map(&:to_s)
end
end
24 changes: 11 additions & 13 deletions spec/geny/actions/git_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,42 @@

describe "#init" do
it "initializes a repo" do
git.init chdir: tmp.to_s, verbose: false
expect(tmp.join(".git")).to be_directory
git.init verbose: false
expect(".git").to be_a_directory
end
end

describe "#add" do
it "adds files" do
write "foo.txt"
git.init chdir: tmp.to_s, verbose: false
git.add chdir: tmp.to_s, verbose: false
git.init verbose: false
git.add verbose: false
expect(added_files).to eq("foo.txt")
end
end

describe "#commit" do
it "commits added files" do
write "foo.txt"
git.init chdir: tmp.to_s, verbose: false
git.add chdir: tmp.to_s, verbose: false
git.commit chdir: tmp.to_s, verbose: false, message: "success"
git.init verbose: false
git.add verbose: false
git.commit verbose: false, message: "success"
expect(last_commit_message).to eq("success\n")
end
end

describe "#repo_path" do
it "determines the location of the git repo" do
git.init(chdir: tmp.to_s, verbose: false)
path = git.repo_path(chdir: tmp.to_s)
path = File.basename(path)
expect(path).to eq(tmp.basename.to_s)
git.init(verbose: false)
expect(git.repo_path).to eq(Dir.pwd)
end
end

def added_files
shell.capture("git diff --cached --name-only", chdir: tmp.to_s)
shell.capture("git diff --cached --name-only")
end

def last_commit_message
shell.capture("git log -1 --pretty=%B", chdir: tmp.to_s)
shell.capture("git log -1 --pretty=%B")
end
end
18 changes: 8 additions & 10 deletions spec/geny/actions/templates_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,32 +69,30 @@ def hello(&block)
describe "#copy" do
it "copies a file" do
write "hello.erb", "hello <%= name %>"
output = tmp.join("hello.txt")

templates = build(locals: {name: "world"})
templates.copy("hello.erb", output, verbose: false)
templates.copy("hello.erb", "hello.txt", verbose: false)

expect(output).to be_file
expect(output.read).to eq("hello world")
expect("hello.txt").to be_a_file
expect("hello.txt").to have_content("hello world")
end
end

describe "#copy_dir" do
it "copies a directory" do
write "hello/%name%.txt.erb", "hello <%= name %>"
output = tmp.join("world.txt")
write "a/%name%.txt.erb", "hello <%= name %>"

templates = build(locals: {name: "world"})
templates.copy_dir("hello", tmp, verbose: false)
templates.copy_dir("a", "b", verbose: false)

expect(output).to be_file
expect(output.read).to eq("hello world")
expect("b/world.txt").to be_a_file
expect("b/world.txt").to have_content("hello world")
end
end

def build(locals: {}, helpers: [])
command = instance_double(Geny::Command, helpers: helpers)
view = Geny::Context::View.new(command: command, locals: locals)
Geny::Actions::Templates.new(root: tmp.to_s, view: view)
Geny::Actions::Templates.new(root: Dir.pwd, view: view)
end
end
3 changes: 1 addition & 2 deletions spec/geny/cli_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
require "tmpdir"
require "geny/cli"

RSpec.describe Geny::CLI do
let(:registry) {
Geny::Registry.new(load_path: [tmp.to_s])
Geny::Registry.new(load_path: [Dir.pwd])
}

subject(:cli) {
Expand Down
22 changes: 9 additions & 13 deletions spec/geny/command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,25 @@
require "geny/registry"

RSpec.describe Geny::Command do
let(:name) { "foo" }
let(:root) { tmp.join("foo").to_s }
let(:file) { tmp.join("foo/generator.rb").to_s }
let(:templates) { tmp.join("foo/templates").to_s }
let(:registry) { instance_double(Geny::Registry) }

subject(:command) {
Geny::Command.new(
name: name,
root: root,
name: "foo",
root: "foo",
registry: registry
)
}

describe "#name" do
it "has a name" do
expect(command.name).to eq(name)
expect(command.name).to eq("foo")
end
end

describe "#root" do
it "has a root" do
expect(command.root).to eq(root)
expect(command.root).to eq("foo")
end
end

Expand All @@ -37,13 +33,13 @@

describe "#file" do
it "has a file" do
expect(command.file).to eq(file)
expect(command.file).to eq("foo/generator.rb")
end
end

describe "#templates" do
it "has a templates_path" do
expect(command.templates_path).to eq(templates)
expect(command.templates_path).to eq("foo/templates")
end
end

Expand All @@ -61,7 +57,7 @@
end

it "is loaded from a file" do
write file, "parse { description 'cool' }"
write "foo/generator.rb", "parse { description 'cool' }"
expect(command.description).to eq("cool")
end
end
Expand Down Expand Up @@ -122,8 +118,8 @@
end

it "is aware of the current __FILE__" do
write file, "invoke { print __FILE__ }"
expect { command.invoke }.to output(file).to_stdout
write "foo/generator.rb", "invoke { print __FILE__ }"
expect { command.invoke }.to output("foo/generator.rb").to_stdout
end

it "raises when invoked with invalid options" do
Expand Down
Loading

0 comments on commit aae02be

Please sign in to comment.