@@ -21,11 +21,13 @@ class MockDSL
2121 :engine => engine ,
2222 :engine_version => engine_version }
2323 end
24+ let ( :project_root ) { Pathname . new ( "/path/to/project" ) }
25+ before { allow ( Bundler ) . to receive ( :root ) . and_return ( project_root ) }
2426
2527 let ( :invoke ) do
2628 proc do
2729 args = [ ]
28- args << Array ( ruby_version_arg ) if ruby_version_arg
30+ args << ruby_version_arg if ruby_version_arg
2931 args << options
3032
3133 dsl . ruby ( *args )
@@ -97,53 +99,67 @@ class MockDSL
9799 end
98100
99101 context "with a file option" do
100- let ( :options ) { { :file => "foo" } }
101- let ( :version ) { "3.2.2" }
102- let ( :ruby_version ) { "3.2.2" }
102+ let ( :file ) { ".ruby-version" }
103+ let ( :options ) do
104+ { :file => file ,
105+ :patchlevel => patchlevel ,
106+ :engine => engine ,
107+ :engine_version => engine_version }
108+ end
103109 let ( :ruby_version_arg ) { nil }
104- let ( :engine_version ) { version }
105- let ( :patchlevel ) { nil }
106- let ( :engine ) { "ruby" }
107- let ( :project_root ) { Pathname . new ( "/path/to/project" ) }
110+ let ( :file_content ) { "#{ version } \n " }
108111
109112 before do
110- allow ( Bundler ) . to receive ( :read_file ) . with ( project_root . join ( "foo" ) ) . and_return ( "#{ version } \n " )
111- allow ( Bundler ) . to receive ( :root ) . and_return ( Pathname . new ( "/path/to/project" ) )
113+ allow ( Bundler ) . to receive ( :read_file ) . with ( project_root . join ( file ) ) . and_return ( file_content )
112114 end
113115
114116 it_behaves_like "it stores the ruby version"
115117
118+ context "with the ruby- prefix in the file" do
119+ let ( :file_content ) { "ruby-#{ version } \n " }
120+
121+ it_behaves_like "it stores the ruby version"
122+ end
123+
116124 context "and a version" do
117- let ( :ruby_version_arg ) { "2.0.0" }
125+ let ( :ruby_version_arg ) { version }
118126
119127 it "raises an error" do
120- expect { subject } . to raise_error ( Bundler ::GemfileError , "Cannot specify version when using the file option" )
128+ expect { subject } . to raise_error ( Bundler ::GemfileError , "Do not pass version argument when using : file option" )
121129 end
122130 end
123- end
124131
125- context "with a (.tool-versions) file option" do
126- let ( :options ) { { :file => "foo" } }
127- let ( :version ) { "3.2.2" }
128- let ( :ruby_version ) { "3.2.2" }
129- let ( :ruby_version_arg ) { nil }
130- let ( :engine_version ) { version }
131- let ( :patchlevel ) { nil }
132- let ( :engine ) { "ruby" }
133- let ( :project_root ) { Pathname . new ( "/path/to/project" ) }
132+ context "with a @gemset" do
133+ let ( :file_content ) { "ruby-#{ version } @gemset\n " }
134134
135- before do
136- allow ( Bundler ) . to receive ( :read_file ) . with ( project_root . join ( "foo" ) ) . and_return ( "nodejs 18.16.0 \n ruby #{ version } # This is a comment \n pnpm 8.6.12 \n ")
137- allow ( Bundler ) . to receive ( :root ) . and_return ( Pathname . new ( "/path/to/project" ) )
135+ it "raises an error" do
136+ expect { subject } . to raise_error ( Gem :: Requirement :: BadRequirementError , "Illformed requirement [ \" #{ version } @gemset \" ] ")
137+ end
138138 end
139139
140- it_behaves_like "it stores the ruby version"
140+ context "with a .tool-versions file format" do
141+ let ( :file ) { ".tool-versions" }
142+ let ( :ruby_version_arg ) { nil }
143+ let ( :file_content ) do
144+ <<~TOOLS
145+ nodejs 18.16.0
146+ ruby #{ version } # This is a comment
147+ pnpm 8.6.12
148+ TOOLS
149+ end
141150
142- context "and a version" do
143- let ( :ruby_version_arg ) { "2.0.0" }
151+ it_behaves_like "it stores the ruby version"
144152
145- it "raises an error" do
146- expect { subject } . to raise_error ( Bundler ::GemfileError , "Cannot specify version when using the file option" )
153+ context "with extra spaces and a very cozy comment" do
154+ let ( :file_content ) do
155+ <<~TOOLS
156+ nodejs 18.16.0
157+ ruby #{ version } # This is a cozy comment
158+ pnpm 8.6.12
159+ TOOLS
160+ end
161+
162+ it_behaves_like "it stores the ruby version"
147163 end
148164 end
149165 end
0 commit comments