Skip to content

Commit

Permalink
Extract file helpers and switch directory before each test
Browse files Browse the repository at this point in the history
  • Loading branch information
rzane committed Dec 26, 2019
1 parent 07491a2 commit 2033099
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 124 deletions.
1 change: 1 addition & 0 deletions geny.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec", "~> 3.0"
spec.add_development_dependency "simplecov", "~> 0.17.1"
spec.add_development_dependency "file_spec", "~> 0.1.0"
end
82 changes: 39 additions & 43 deletions spec/geny/actions/files_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,123 +2,119 @@
require "geny/actions/ui"

RSpec.describe Geny::Actions::Files do
let(:file) { tmp.join("foo.txt") }
let(:dir) { tmp.join("foo") }
let(:ui) { instance_double(Geny::Actions::UI, status: nil) }

subject(:files) { described_class.new(ui: ui) }

describe "#create" do
it "creates a file" do
files.create(file, "hi", verbose: false)
expect(file).to be_file
expect(file.read).to eq("hi")
files.create("foo.txt", "hi", verbose: false)
expect("foo.txt").to be_a_file
expect("foo.txt").to have_content("hi")
end
end

describe "#remove" do
it "removes a file" do
write file
files.remove(file, verbose: false)
expect(file).not_to be_a_file
write "foo.txt"
files.remove("foo.txt", verbose: false)
expect("foo.txt").not_to be_a_file
end
end

describe "#create" do
it "creates a dir" do
files.create_dir(dir, verbose: false)
expect(dir).to be_directory
files.create_dir("foo", verbose: false)
expect("foo").to be_a_directory
end
end

describe "#prepend" do
it "prepends a file" do
write file, "bye"
files.prepend(file, "hi\n", verbose: false)
expect(file.read).to eq("hi\nbye")
write "foo.txt", "bye"
files.prepend("foo.txt", "hi\n", verbose: false)
expect("foo.txt").to have_content("hi\nbye")
end
end

describe "#append" do
it "appends a file" do
write file, "hi\n"
files.append(file, "bye", verbose: false)
expect(file.read).to eq("hi\nbye")
write "foo.txt", "hi\n"
files.append("foo.txt", "bye", verbose: false)
expect("foo.txt").to have_content("hi\nbye")
end
end

describe "#replace" do
it "replaces a file" do
write file, "hi"
files.replace(file, /hi/, "bye", verbose: false)
expect(file.read).to eq("bye")
write "foo.txt", "hi"
files.replace("foo.txt", /hi/, "bye", verbose: false)
expect("foo.txt").to have_content("bye")
end
end

describe "#insert" do
it "inserts before a matching line in a file" do
write file, "bye"
files.insert(file, "hi", before: /bye/, verbose: false)
expect(file.read).to eq("hibye")
write "foo.txt", "bye"
files.insert("foo.txt", "hi", before: /bye/, verbose: false)
expect("foo.txt").to have_content("hibye")
end

it "inserts after a matching line in a file" do
write file, "hi"
files.insert(file, "bye", after: /hi/, verbose: false)
expect(file.read).to eq("hibye")
write "foo.txt", "hi"
files.insert("foo.txt", "bye", after: /hi/, verbose: false)
expect("foo.txt").to have_content("hibye")
end
end

describe "#insert_before" do
it "inserts before a matching line in a file" do
write file, "bye"
files.insert_before(file, /bye/, "hi", verbose: false)
expect(file.read).to eq("hibye")
write "foo.txt", "bye"
files.insert_before("foo.txt", /bye/, "hi", verbose: false)
expect("foo.txt").to have_content("hibye")
end
end

describe "#insert_after" do
it "inserts after a matching line in a file" do
write file, "hi"
files.insert_after(file, /hi/, "bye", verbose: false)
expect(file.read).to eq("hibye")
write "foo.txt", "hi"
files.insert_after("foo.txt", /hi/, "bye", verbose: false)
expect("foo.txt").to have_content("hibye")
end
end

describe "#move" do
it "moves a file" do
write "foo.txt"
files.move(tmp.join("foo.txt"), tmp.join("bar.txt"), verbose: false)
expect(tmp.join("bar.txt")).to be_file
expect(tmp.join("foo.txt")).not_to be_file
files.move("foo.txt", "bar.txt", verbose: false)
expect("bar.txt").to be_a_file
expect("foo.txt").not_to be_a_file
end
end

describe "#chdir" do
it "changes directory" do
write "foo/bar.txt", "baz"
result = files.chdir(tmp.join("foo"), verbose: false) do
File.read("bar.txt")
end
result = files.chdir("foo", verbose: false) { read("bar.txt") }
expect(result).to eq("baz")
end
end

describe "#chmod" do
it "chmods a file" do
write file
write "foo.txt"

expect {
files.chmod(file, 0777, verbose: false)
}.to change { file.lstat.mode }
files.chmod("foo.txt", 0777, verbose: false)
}.to change { File.lstat("foo.txt").mode }
end

it "chmods a file with +x" do
write file
write "foo.txt"

expect {
files.chmod(file, "+x", verbose: false)
}.to change { file.lstat.mode }
files.chmod("foo.txt", "+x", verbose: false)
}.to change { File.lstat("foo.txt").mode }
end
end
end
35 changes: 14 additions & 21 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(tmp.to_s, "hello", "goodbye", excluding: /b/)
expect(tmp.join("a.txt").read).to eq("goodbye")
expect(tmp.join("b.txt").read).to eq("hello")
find.replace(".", "hello", "goodbye", excluding: /b/)
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
Loading

0 comments on commit 2033099

Please sign in to comment.