diff --git a/geny.gemspec b/geny.gemspec index 78db8c2..7109fe1 100644 --- a/geny.gemspec +++ b/geny.gemspec @@ -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 diff --git a/spec/geny/actions/files_spec.rb b/spec/geny/actions/files_spec.rb index 3184bcd..9a334df 100644 --- a/spec/geny/actions/files_spec.rb +++ b/spec/geny/actions/files_spec.rb @@ -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 diff --git a/spec/geny/actions/find_spec.rb b/spec/geny/actions/find_spec.rb index c22dfde..d79c04d 100644 --- a/spec/geny/actions/find_spec.rb +++ b/spec/geny/actions/find_spec.rb @@ -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 diff --git a/spec/geny/actions/git_spec.rb b/spec/geny/actions/git_spec.rb index 2240557..b5bf71f 100644 --- a/spec/geny/actions/git_spec.rb +++ b/spec/geny/actions/git_spec.rb @@ -9,16 +9,16 @@ 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 @@ -26,27 +26,25 @@ 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 diff --git a/spec/geny/actions/templates_spec.rb b/spec/geny/actions/templates_spec.rb index b857da5..25f6cef 100644 --- a/spec/geny/actions/templates_spec.rb +++ b/spec/geny/actions/templates_spec.rb @@ -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 diff --git a/spec/geny/cli_spec.rb b/spec/geny/cli_spec.rb index db28b26..f16b669 100644 --- a/spec/geny/cli_spec.rb +++ b/spec/geny/cli_spec.rb @@ -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) { diff --git a/spec/geny/command_spec.rb b/spec/geny/command_spec.rb index e216c08..2bd08d2 100644 --- a/spec/geny/command_spec.rb +++ b/spec/geny/command_spec.rb @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/spec/geny/registry_spec.rb b/spec/geny/registry_spec.rb index f9e018a..cc10370 100644 --- a/spec/geny/registry_spec.rb +++ b/spec/geny/registry_spec.rb @@ -2,7 +2,7 @@ RSpec.describe Geny::Registry do subject(:registry) { - Geny::Registry.new(load_path: [tmp.to_s]) + Geny::Registry.new(load_path: ["."]) } before do @@ -21,7 +21,7 @@ it "finds a generator" do command = registry.find("a:b") expect(command.name).to eq("a:b") - expect(command.root).to eq(tmp.join("a/b").to_s) + expect(command.root).to eq("./a/b") end it "is nil when the generator is not found" do @@ -33,7 +33,7 @@ it "finds a generator" do command = registry.find!("a:b") expect(command.name).to eq("a:b") - expect(command.root).to eq(tmp.join("a/b").to_s) + expect(command.root).to eq("./a/b") end it "raises when the generator is not found" do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 008b910..a09c911 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,7 +1,7 @@ require "bundler/setup" require_relative "support/coverage" require "geny" -require "tmpdir" +require "file_spec" module Helpers def module_double(opts) @@ -15,28 +15,11 @@ def module_double(opts) end mod end - - def tmp - @tmp ||= Pathname.new(Dir.mktmpdir) - end - - def purge_tmp - @tmp.rmtree if defined?(@tmp) - end - - def write(filename, content = "") - path = tmp.join(filename) - path.parent.mkpath - path.write(content) - end end RSpec.configure do |config| config.include Helpers - - config.after :each do - purge_tmp - end + config.include FileSpec config.around :each do |example| begin