Skip to content

Commit

Permalink
- Fix example copy
Browse files Browse the repository at this point in the history
  • Loading branch information
DannyBen committed Jan 26, 2023
1 parent 4b87e99 commit ec1bc1f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 16 deletions.
23 changes: 13 additions & 10 deletions lib/runfile/initiator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ def list_examples

def create_example(name)
dir = "#{examples_dir}/#{name}"
files = Dir["#{dir}/{runfile,*.runfile,*.rb}"]
raise UserError, "No such example: nu`#{name}`" if files.empty?
raise UserError, "No such example: nu`#{name}`" unless Dir.exist? dir

files.each { |file| safe_copy file }
files = Dir.chdir(dir) { Dir["**/{runfile,*.runfile,*.rb}"] }
files.each { |file| safe_copy dir, file }
say_tip
end

Expand All @@ -59,14 +59,17 @@ def say_tip
say 'Delete the copied files to go back to the initial state'
end

def safe_copy(file)
target = File.basename file
# This will never happen since if there is a runfile, the initiator will
# not be called. Nonetheless, keep it for safety
return if File.exist? target
def safe_copy(source_dir, target_file)
source_file = "#{source_dir}/#{target_file}"

FileUtils.cp file, '.'
say "Copied g`#{target}`"
if File.exist? target_file
say "r`Skipped #{target_file}` (exists)"
return
end

FileUtils.mkdir_p File.dirname(target_file)
FileUtils.cp source_file, target_file
say "Copied g`#{target_file}`"
end

def examples
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Copied runfile
Copied more_tasks/spec.runfile
Copied runfile
Copied tasks/server.runfile

Run run or run --help to see your runfile
Delete the copied files to go back to the initial state
6 changes: 6 additions & 0 deletions spec/approvals/initiator/example-import-skip
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Copied more_tasks/spec.runfile
Copied runfile
Skipped tasks/server.runfile (exists)

Run run or run --help to see your runfile
Delete the copied files to go back to the initial state
29 changes: 24 additions & 5 deletions spec/runfile/initiator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,31 @@
end
end

context 'with "example minimal"' do
it 'copies the example file(s) to the current directory' do
context 'with "example EXAMPLE"' do
before do
Dir['spec/tmp/**/{runfile,*.runfile,*.rb}'].each { |f| FileUtils.rm f }
end

it 'copies the example files to the current directory' do
Dir.chdir 'spec/tmp' do
expect { subject.run %w[example minimal] }.to output_approval('initiator/example-minimal')
expect(File).to exist 'runfile'
expect(File.read 'runfile').to eq File.read('../../examples/minimal/runfile')
expect { subject.run %w[example import] }.to output_approval('initiator/example-import')
actual = Dir['**/*'].select { |f| File.file? f }.sort
expected = Dir.chdir('../../examples/import') { Dir['**/*'] }.select { |f| File.file? f }.sort
expect(actual).to eq expected
end
end

context 'when one of the files already exists' do
before do
FileUtils.mkdir_p 'spec/tmp/tasks'
File.write 'spec/tmp/tasks/server.runfile', 'original content'
end

it 'does not overwrite it' do
Dir.chdir 'spec/tmp' do
expect { subject.run %w[example import] }.to output_approval('initiator/example-import-skip')
expect(File.read 'tasks/server.runfile').to eq 'original content'
end
end
end
end
Expand Down

0 comments on commit ec1bc1f

Please sign in to comment.