Skip to content

Commit 51adce4

Browse files
committed
Ensure we require Pathname in standalone mode too
1 parent ef70638 commit 51adce4

File tree

4 files changed

+28
-16
lines changed

4 files changed

+28
-16
lines changed

lib/git_tracker/commit_message.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require "pathname"
2+
13
module GitTracker
24
class CommitMessage
35
def initialize(file)

lib/git_tracker/hook.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require "pathname"
12
require "git_tracker/repository"
23

34
module GitTracker

lib/git_tracker/standalone.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
require "pathname"
2+
13
module GitTracker
24
module Standalone
35
extend self
46

5-
GIT_TRACKER_ROOT = File.expand_path("../../..", __FILE__)
7+
GIT_TRACKER_ROOT = Pathname(__dir__).join("../..")
68
PREAMBLE = <<~DOC
79
#
810
# This file is generated code. DO NOT send patches for it.
@@ -17,7 +19,7 @@ def build(io)
1719
io.puts(PREAMBLE)
1820

1921
each_source_file do |filename|
20-
File.open(filename, "r") do |source|
22+
Pathname(filename).open("r") do |source|
2123
inline_source(source, io)
2224
end
2325
end
@@ -26,9 +28,9 @@ def build(io)
2628
io
2729
end
2830

29-
def save(filename, path = ".")
30-
dest = File.join(File.expand_path(path), filename)
31-
File.open(dest, "w") do |f|
31+
def save(filename, path: ".")
32+
dest = Pathname(path).join(filename).expand_path
33+
dest.open("w") do |f|
3234
build(f)
3335
f.chmod(0o755)
3436
end
@@ -37,10 +39,10 @@ def save(filename, path = ".")
3739
private
3840

3941
def each_source_file
40-
File.open(File.join(GIT_TRACKER_ROOT, "lib/git_tracker.rb"), "r") do |main|
42+
GIT_TRACKER_ROOT.join("lib/git_tracker.rb").open("r") do |main|
4143
main.each_line do |req|
4244
if req =~ /^require\s+["'](.+)["']/
43-
yield File.join(GIT_TRACKER_ROOT, "lib", "#{$1}.rb")
45+
yield GIT_TRACKER_ROOT.join("lib/#{$1}.rb").to_path
4446
end
4547
end
4648
end

spec/git_tracker/standalone_spec.rb

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,25 @@
22

33
RSpec.describe GitTracker::Standalone do
44
describe "#save" do
5-
before do
6-
File.delete "git-tracker" if File.exist? "git-tracker"
7-
end
5+
let(:binary) { Pathname(pkg_dir).join("git-tracker") }
6+
let(:pkg_dir) { Pathname(@pkg_dir).to_path }
87

9-
after do
10-
File.delete "git-tracker" if File.exist? "git-tracker"
8+
around do |example|
9+
Dir.mktmpdir do |dir|
10+
@pkg_dir = dir
11+
example.call
12+
remove_instance_variable(:@pkg_dir)
13+
end
1114
end
1215

1316
it "saves to the named file" do
14-
described_class.save("git-tracker")
15-
expect(File.size("./git-tracker")).to be > 100
17+
described_class.save("git-tracker", path: pkg_dir)
18+
expect(binary.size).to be > 100
1619
end
1720

1821
it "marks the binary as executable" do
19-
described_class.save("git-tracker")
20-
expect(File).to be_executable("./git-tracker")
22+
described_class.save("git-tracker", path: pkg_dir)
23+
expect(binary).to be_executable
2124
end
2225
end
2326

@@ -59,6 +62,10 @@
5962
expect(standalone_script).to include("GitTracker::Runner.call(*ARGV)")
6063
end
6164

65+
it "includes requiring code from stdlib" do
66+
expect(standalone_script).to match(/^require\s+["']pathname/)
67+
end
68+
6269
it "excludes requiring git_tracker code" do
6370
expect(standalone_script).to_not match(/^require\s+["']git_tracker/)
6471
end

0 commit comments

Comments
 (0)