Skip to content

Commit ec1bc1f

Browse files
committed
- Fix example copy
1 parent 4b87e99 commit ec1bc1f

File tree

4 files changed

+46
-16
lines changed

4 files changed

+46
-16
lines changed

lib/runfile/initiator.rb

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ def list_examples
4646

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

52-
files.each { |file| safe_copy file }
51+
files = Dir.chdir(dir) { Dir["**/{runfile,*.runfile,*.rb}"] }
52+
files.each { |file| safe_copy dir, file }
5353
say_tip
5454
end
5555

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

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

68-
FileUtils.cp file, '.'
69-
say "Copied g`#{target}`"
65+
if File.exist? target_file
66+
say "r`Skipped #{target_file}` (exists)"
67+
return
68+
end
69+
70+
FileUtils.mkdir_p File.dirname(target_file)
71+
FileUtils.cp source_file, target_file
72+
say "Copied g`#{target_file}`"
7073
end
7174

7275
def examples
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
Copied runfile
1+
Copied more_tasks/spec.runfile
2+
Copied runfile
3+
Copied tasks/server.runfile
24

35
Run run or run --help to see your runfile
46
Delete the copied files to go back to the initial state
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Copied more_tasks/spec.runfile
2+
Copied runfile
3+
Skipped tasks/server.runfile (exists)
4+
5+
Run run or run --help to see your runfile
6+
Delete the copied files to go back to the initial state

spec/runfile/initiator_spec.rb

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,31 @@
2626
end
2727
end
2828

29-
context 'with "example minimal"' do
30-
it 'copies the example file(s) to the current directory' do
29+
context 'with "example EXAMPLE"' do
30+
before do
31+
Dir['spec/tmp/**/{runfile,*.runfile,*.rb}'].each { |f| FileUtils.rm f }
32+
end
33+
34+
it 'copies the example files to the current directory' do
3135
Dir.chdir 'spec/tmp' do
32-
expect { subject.run %w[example minimal] }.to output_approval('initiator/example-minimal')
33-
expect(File).to exist 'runfile'
34-
expect(File.read 'runfile').to eq File.read('../../examples/minimal/runfile')
36+
expect { subject.run %w[example import] }.to output_approval('initiator/example-import')
37+
actual = Dir['**/*'].select { |f| File.file? f }.sort
38+
expected = Dir.chdir('../../examples/import') { Dir['**/*'] }.select { |f| File.file? f }.sort
39+
expect(actual).to eq expected
40+
end
41+
end
42+
43+
context 'when one of the files already exists' do
44+
before do
45+
FileUtils.mkdir_p 'spec/tmp/tasks'
46+
File.write 'spec/tmp/tasks/server.runfile', 'original content'
47+
end
48+
49+
it 'does not overwrite it' do
50+
Dir.chdir 'spec/tmp' do
51+
expect { subject.run %w[example import] }.to output_approval('initiator/example-import-skip')
52+
expect(File.read 'tasks/server.runfile').to eq 'original content'
53+
end
3554
end
3655
end
3756
end

0 commit comments

Comments
 (0)