Skip to content

Commit 325791c

Browse files
committed
refactor Renderable#load_user_file
1 parent 35d99ed commit 325791c

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

lib/bashly/concerns/renderable.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ def view_marker(id = nil)
1818
# Reads a file from the userspace (Settings.source_dir) and returns
1919
# its contents. If the file is not found, returns a string with a hint.
2020
def load_user_file(file, placeholder: true)
21-
path = "#{Settings.source_dir}/#{file}"
22-
ext = ".#{Settings.partials_extension}"
23-
path += ext unless path.end_with? ext
21+
path = user_file_path file
2422

2523
content = if File.exist? path
2624
File.read(path).remove_front_matter
@@ -33,6 +31,14 @@ def load_user_file(file, placeholder: true)
3331
Settings.production? ? content : "#{view_marker path}\n#{content}"
3432
end
3533

34+
def user_file_path(file)
35+
path = "#{Settings.source_dir}/#{file}"
36+
ext = ".#{Settings.partials_extension}"
37+
return path if path.end_with? ext
38+
39+
"#{path}#{ext}"
40+
end
41+
3642
private
3743

3844
def view_path(view)

spec/bashly/script/command_spec.rb

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,25 @@
330330
.to eq "# spec/tmp/src/notfound.sh\necho \"error: cannot load file\""
331331
end
332332
end
333+
end
334+
335+
describe '#user_file_path' do
336+
it 'returns the path to the user file' do
337+
expect(subject.user_file_path 'test.sh').to eq 'spec/tmp/src/test.sh'
338+
end
339+
340+
context 'when the file argument does not end with .sh extension' do
341+
it 'returns the path with .sh appended' do
342+
expect(subject.user_file_path 'test').to eq 'spec/tmp/src/test.sh'
343+
end
344+
end
345+
346+
context 'when partials_extension is set and the argument does not end with the selected extension' do
347+
before { Settings.partials_extension = 'bash' }
348+
after { Settings.partials_extension = 'sh' }
333349

334-
context 'when the provided file does not include an extension' do
335-
it 'adds the partials_extension to it' do
336-
expect(subject.load_user_file('test')).to eq "# spec/tmp/src/test.sh\nhello Command#load_user_file"
350+
it 'returns the path with the selected extension appended' do
351+
expect(subject.user_file_path 'test').to eq 'spec/tmp/src/test.bash'
337352
end
338353
end
339354
end

0 commit comments

Comments
 (0)