Skip to content

Commit 3540c7c

Browse files
committed
wip test fix against CI
1 parent 3dd5d4b commit 3540c7c

File tree

5 files changed

+117
-101
lines changed

5 files changed

+117
-101
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ jobs:
7171
bundler-cache: true
7272
rubygems: ${{ matrix.rubygems }}
7373
- name: Run tests
74-
run: bin/test
74+
run: bin/test spec/tapioca/ruby_lsp/run_gem_rbi_check_spec.rb --seed 13598
7575
continue-on-error: ${{ !!matrix.experimental }}
7676

7777
buildall:

lib/ruby_lsp/tapioca/run_gem_rbi_check.rb

+18-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ def initialize
2727
sig { params(project_path: String).returns(GemRbiCheckResult) }
2828
def run(project_path = ".")
2929
FileUtils.chdir(project_path) do
30+
$stderr.puts "Inside Run Method"
3031
if git_repo?
32+
$stderr.puts "Inside git_repo? Check"
3133
lockfile_changed? ? generate_gem_rbis : cleanup_orphaned_rbis
3234
else
3335
log_message("Not a git repository")
@@ -49,25 +51,30 @@ def git_repo?
4951

5052
sig { returns(T::Boolean) }
5153
def lockfile_changed?
54+
$stdout.puts "Inside lockfile_changed? Method"
5255
fetch_lockfile_diff
53-
!T.must(@lockfile_diff).empty?
56+
$stdout.puts "fetch_lockfile_diff = #{fetch_lockfile_diff}"
57+
!@lockfile_diff.empty?
5458
end
5559

5660
sig { returns(String) }
5761
def fetch_lockfile_diff
58-
@lockfile_diff = %x(git diff HEAD Gemfile.lock).strip
62+
@lockfile_diff = File.exist?("Gemfile.lock") ? %x(git diff Gemfile.lock).strip : ""
5963
end
6064

6165
sig { void }
6266
def generate_gem_rbis
67+
$stdout.puts "Inside generate_gem_rbis Method"
6368
parser = Tapioca::LockfileDiffParser.new(@lockfile_diff)
6469
removed_gems = parser.removed_gems
6570
added_or_modified_gems = parser.added_or_modified_gems
6671

6772
if added_or_modified_gems.any?
73+
$stdout.puts "Inside if added_or_modified_gems.any? Check"
6874
log_message("Identified lockfile changes, attempting to generate gem RBIs...")
6975
execute_tapioca_gem_command(added_or_modified_gems)
7076
elsif removed_gems.any?
77+
$stdout.puts "Inside elsif removed_gems.any? Check"
7178
remove_rbis(removed_gems)
7279
end
7380
end
@@ -77,7 +84,9 @@ def execute_tapioca_gem_command(gems)
7784
# Resetting BUNDLE_GEMFILE to root folder to use the project's Gemfile instead of Ruby LSP's composed Gemfile
7885
stdout, stderr, status = T.unsafe(Open3).capture3(
7986
{ "BUNDLE_GEMFILE" => "Gemfile" },
80-
"bin/tapioca",
87+
"bundle",
88+
"exec",
89+
"tapioca",
8190
"gem",
8291
"--lsp_addon",
8392
*gems,
@@ -96,27 +105,33 @@ def remove_rbis(gems)
96105

97106
sig { void }
98107
def cleanup_orphaned_rbis
108+
$stdout.puts "Inside cleanup_orphaned_rbis Method"
99109
untracked_files = %x(git ls-files --others --exclude-standard sorbet/rbi/gems/).lines.map(&:strip)
100110
deleted_files = %x(git ls-files --deleted sorbet/rbi/gems/).lines.map(&:strip)
111+
$stdout.puts "untracked_files = #{untracked_files}"
112+
$stdout.puts "deleted_files = #{deleted_files}"
101113

102114
delete_files(untracked_files, "Deleted untracked RBIs")
103115
restore_files(deleted_files, "Restored deleted RBIs")
104116
end
105117

106118
sig { params(files: T::Array[String], message: String).void }
107119
def delete_files(files, message)
120+
$stdout.puts "Inside delete_files Method"
108121
files.each { |file| File.delete(file) }
109122
log_message("#{message}: #{files.join(", ")}") unless files.empty?
110123
end
111124

112125
sig { params(files: T::Array[String], message: String).void }
113126
def restore_files(files, message)
127+
$stdout.puts "Inside restore_files Method"
114128
files.each { |file| %x(git checkout -- #{file}) }
115129
log_message("#{message}: #{files.join(", ")}") unless files.empty?
116130
end
117131

118132
sig { params(message: String).void }
119133
def log_message(message)
134+
$stderr.puts message
120135
@result.stdout += "#{message}\n"
121136
end
122137
end

spec/helpers/mock_gem.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class MockGem < Spoom::Context
1212

1313
# The gem's version string such as "1.0.0" or ">= 2.0.5"
1414
sig { returns(String) }
15-
attr_reader :version
15+
attr_accessor :version
1616

1717
# The dependencies to be added to the gem's gemspec
1818
sig { returns(T::Array[String]) }

spec/spec_with_project.rb

+7
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ def mock_gem(name, version, dependencies: [], path: default_gem_path(name), &blo
8484
gem
8585
end
8686

87+
sig { params(gem: MockGem, version: String).returns(MockGem) }
88+
def update_gem(gem, version)
89+
gem.version = version
90+
gem.gemspec(gem.default_gemspec_contents)
91+
gem
92+
end
93+
8794
# Spec assertions
8895

8996
# Assert that the contents of `path` inside `@project` is equals to `expected`

spec/tapioca/ruby_lsp/run_gem_rbi_check_spec.rb

+90-96
Original file line numberDiff line numberDiff line change
@@ -12,140 +12,134 @@ module Foo
1212
end
1313
RUBY
1414

15-
# TODO: understand why this fails with `before(:all)`
16-
# before(:all) do
1715
before do
18-
@project.tapioca("configure")
16+
@project = mock_project
1917
end
2018

2119
after do
22-
project.write_gemfile!(project.tapioca_gemfile)
23-
@project.require_default_gems
24-
project.remove!("sorbet/rbi")
25-
project.remove!("../gems")
26-
project.remove!(".git")
27-
project.remove!("sorbet/tapioca/require.rb")
28-
project.remove!("config/application.rb")
29-
ensure
30-
@project.remove!("output")
20+
@project.destroy!
3121
end
3222

33-
def setup_git_repo
34-
@project.exec("git init")
35-
@project.exec("touch Gemfile.lock")
36-
FileUtils.mkdir_p("#{@project.absolute_path}/sorbet/rbi/gems")
37-
@project.exec("git add . && git commit -m 'Initial commit'")
38-
end
39-
40-
it "does nothing if there is no git repo" do
41-
foo = mock_gem("foo", "0.0.1") do
42-
write!("lib/foo.rb", FOO_RB)
23+
describe "without git" do
24+
before do
25+
@project.bundle_install!
26+
@project.tapioca("configure")
4327
end
44-
@project.require_mock_gem(foo)
4528

46-
@project.bundle_install!
47-
check = ::RubyLsp::Tapioca::RunGemRbiCheck.new
48-
check.run(@project.absolute_path)
49-
50-
assert check.result.stdout.include?("Not a git repository")
51-
end
29+
it "does nothing if there is no git repo" do
30+
foo = mock_gem("foo", "0.0.1") do
31+
write!("lib/foo.rb", FOO_RB)
32+
end
33+
@project.require_mock_gem(foo)
5234

53-
it "creates the RBI for a newly added gem" do
54-
setup_git_repo
35+
@project.bundle_install!
36+
check = ::RubyLsp::Tapioca::RunGemRbiCheck.new
37+
check.run(@project.absolute_path)
5538

56-
foo = mock_gem("foo", "0.0.1") do
57-
write!("lib/foo.rb", FOO_RB)
39+
assert check.result.stdout.include?("Not a git repository")
5840
end
59-
@project.require_mock_gem(foo)
60-
@project.bundle_install!
61-
62-
check = ::RubyLsp::Tapioca::RunGemRbiCheck.new
63-
check.run(@project.absolute_path)
64-
65-
assert_project_file_exist("sorbet/rbi/gems/[email protected]")
6641
end
6742

68-
it "regenerates RBI when a gem version changes" do
69-
setup_git_repo
70-
71-
foo = mock_gem("foo", "0.0.1") do
72-
write!("lib/foo.rb", FOO_RB)
43+
describe "with git" do
44+
before do
45+
@project.bundle_install!
46+
@project.tapioca("configure")
47+
@project.exec("git init")
48+
@project.exec("git config commit.gpgsign false")
49+
@project.exec("git add .")
50+
@project.exec("git commit -m 'Initial commit'")
7351
end
74-
@project.require_mock_gem(foo)
75-
@project.bundle_install!
7652

77-
check = ::RubyLsp::Tapioca::RunGemRbiCheck.new
78-
check.run(@project.absolute_path)
53+
it "creates the RBI for a newly added gem" do
54+
foo = mock_gem("foo", "0.0.1") do
55+
write!("lib/foo.rb", FOO_RB)
56+
end
57+
@project.require_mock_gem(foo)
58+
@project.bundle_install!
7959

80-
assert_project_file_exist("sorbet/rbi/gems/[email protected]")
60+
check = ::RubyLsp::Tapioca::RunGemRbiCheck.new
61+
check.run(@project.absolute_path)
8162

82-
# Modify the gem
83-
foo = mock_gem("foo", "0.0.2") do
84-
write!("lib/foo.rb", FOO_RB)
63+
assert_project_file_exist("sorbet/rbi/gems/[email protected]")
8564
end
86-
@project.require_mock_gem(foo)
87-
@project.bundle_install!
8865

89-
check.run(@project.absolute_path)
66+
it "regenerates RBI when a gem version changes" do
67+
foo = mock_gem("foo", "0.0.1") do
68+
write!("lib/foo.rb", FOO_RB)
69+
end
70+
@project.require_mock_gem(foo)
71+
@project.bundle_install!
9072

91-
assert_project_file_exist("sorbet/rbi/gems/[email protected]")
92-
end
73+
check = ::RubyLsp::Tapioca::RunGemRbiCheck.new
74+
check.run(@project.absolute_path)
9375

94-
it "removes RBI file when a gem is removed" do
95-
setup_git_repo
76+
assert_project_file_exist("sorbet/rbi/gems/[email protected]")
9677

97-
foo = mock_gem("foo", "0.0.1") do
98-
write!("lib/foo.rb", FOO_RB)
99-
end
100-
@project.require_mock_gem(foo)
101-
@project.bundle_install!
78+
# Modify the gem
79+
update_gem foo, "0.0.2"
80+
@project.bundle_install!
81+
82+
check.run(@project.absolute_path)
10283

103-
check = ::RubyLsp::Tapioca::RunGemRbiCheck.new
104-
check.run(@project.absolute_path)
84+
assert_project_file_exist("sorbet/rbi/gems/[email protected]")
85+
end
10586

106-
assert_project_file_exist("sorbet/rbi/gems/[email protected]")
87+
it "removes RBI file when a gem is removed" do
88+
foo = mock_gem("foo", "0.0.1") do
89+
write!("lib/foo.rb", FOO_RB)
90+
end
91+
@project.require_mock_gem(foo)
92+
@project.bundle_install!
10793

108-
@project.exec("git add Gemfile.lock")
109-
@project.exec("git commit -m 'Add foo gem'")
94+
check1 = ::RubyLsp::Tapioca::RunGemRbiCheck.new
95+
check1.run(@project.absolute_path)
11096

111-
@project.write_gemfile!(@project.tapioca_gemfile)
112-
@project.bundle_install!
97+
assert_project_file_exist("sorbet/rbi/gems/[email protected]")
11398

114-
check = ::RubyLsp::Tapioca::RunGemRbiCheck.new
115-
check.run(@project.absolute_path)
99+
@project.exec("git restore Gemfile Gemfile.lock")
116100

117-
refute_project_file_exist("sorbet/rbi/gems/[email protected]")
118-
end
101+
check2 = ::RubyLsp::Tapioca::RunGemRbiCheck.new
102+
check2.run(@project.absolute_path)
119103

120-
it "deletes untracked RBI files" do
121-
setup_git_repo
104+
refute_project_file_exist("sorbet/rbi/gems/[email protected]")
105+
end
122106

123-
# Create an untracked RBI file
124-
FileUtils.touch("#{@project.absolute_path}/sorbet/rbi/gems/[email protected]")
107+
it "deletes untracked RBI files" do
108+
$stdout.puts @project.exec("git log --oneline")
109+
@project.bundle_install!
110+
FileUtils.mkdir_p("#{@project.absolute_path}/sorbet/rbi/gems")
111+
# Create an untracked RBI file
112+
FileUtils.touch("#{@project.absolute_path}/sorbet/rbi/gems/[email protected]")
125113

126-
assert_project_file_exist("/sorbet/rbi/gems/[email protected]")
114+
assert_project_file_exist("/sorbet/rbi/gems/[email protected]")
127115

128-
check = ::RubyLsp::Tapioca::RunGemRbiCheck.new
129-
check.run(@project.absolute_path)
116+
check = ::RubyLsp::Tapioca::RunGemRbiCheck.new
117+
check.run(@project.absolute_path)
130118

131-
refute_project_file_exist("sorbet/rbi/gems/[email protected]")
132-
end
119+
refute_project_file_exist("sorbet/rbi/gems/[email protected]")
120+
end
133121

134-
it "restores deleted RBI files" do
135-
setup_git_repo
122+
it "restores deleted RBI files" do
123+
@project.bundle_install!
124+
FileUtils.mkdir_p("#{@project.absolute_path}/sorbet/rbi/gems")
125+
# Create and delete a tracked RBI file
126+
FileUtils.touch("#{@project.absolute_path}/sorbet/rbi/gems/[email protected]")
127+
@project.exec("git add sorbet/rbi/gems/[email protected]")
128+
@project.exec("git commit -m 'Add foo RBI'")
129+
FileUtils.rm("#{@project.absolute_path}/sorbet/rbi/gems/[email protected]")
130+
$stdout.puts @project.exec("git log --oneline")
136131

137-
# Create and delete a tracked RBI file
138-
FileUtils.touch("#{@project.absolute_path}/sorbet/rbi/gems/[email protected]")
139-
@project.exec("git add sorbet/rbi/gems/[email protected]")
140-
@project.exec("git commit -m 'Add foo RBI'")
141-
FileUtils.rm("#{@project.absolute_path}/sorbet/rbi/gems/[email protected]")
132+
refute_project_file_exist("sorbet/rbi/gems/[email protected]")
142133

143-
refute_project_file_exist("sorbet/rbi/gems/[email protected]")
134+
check = ::RubyLsp::Tapioca::RunGemRbiCheck.new
135+
check.run(@project.absolute_path)
144136

145-
check = ::RubyLsp::Tapioca::RunGemRbiCheck.new
146-
check.run(@project.absolute_path)
137+
assert_project_file_exist("sorbet/rbi/gems/[email protected]")
147138

148-
assert_project_file_exist("sorbet/rbi/gems/[email protected]")
139+
# Clean-up commit
140+
@project.exec("git reset --hard HEAD^")
141+
$stdout.puts @project.exec("git log --oneline")
142+
end
149143
end
150144
end
151145
end

0 commit comments

Comments
 (0)