Skip to content

Commit

Permalink
Add chdir and move
Browse files Browse the repository at this point in the history
  • Loading branch information
rzane committed Dec 23, 2019
1 parent 747b4bf commit 19615e3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
29 changes: 29 additions & 0 deletions lib/geny/actions/files.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ module Actions
class Files
extend Forwardable

def initialize(ui:)
@ui = ui
end

# @!method create
# Create a file
# @see TTY::File.create
Expand Down Expand Up @@ -68,6 +72,31 @@ class Files
# files.insert("Gemfile", "gem 'geny'", after: /gem 'rails'\n/)
def_delegator TTY::File, :safe_inject_into_file, :insert

# Move a file
# @param source [String]
# @param dest [String]
#
# @example
# files.move "Gemfile", "Gemfile.bak"
def move(source, dest, force: false, verbose: true)
@ui.status("move", source) if verbose
FileUtils.mv(source, dest, force: force)
end

# Change directory
# @param path [String]
#
# @example
# files.chdir "foo" do
# # do something inside foo/
# end
def chdir(path, verbose: true, &block)
@ui.status("cd", path) if verbose
Dir.chdir(path, &block)
ensure
@ui.status("cd", "-") if verbose
end

# Change the permissions of a file
# @see TTY::File.chmod
#
Expand Down
2 changes: 1 addition & 1 deletion lib/geny/context/invoke.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def ui
# A utility for interacting with files
# @return [Actions::Files]
def files
Actions::Files.new
Actions::Files.new(ui: ui)
end

# A utility for bulk find-and-replace operations
Expand Down
23 changes: 22 additions & 1 deletion spec/geny/actions/files_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
require "geny/actions/files"
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 }
subject(:files) { described_class.new(ui: ui) }

describe "#create" do
it "creates a file" do
Expand Down Expand Up @@ -83,6 +85,25 @@
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
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
expect(result).to eq("baz")
end
end

describe "#chmod" do
it "chmods a file" do
write file
Expand Down

0 comments on commit 19615e3

Please sign in to comment.