-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathpython_version_spec.rb
428 lines (388 loc) · 20 KB
/
python_version_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# frozen_string_literal: true
require_relative '../spec_helper'
RSpec.shared_examples 'builds with the requested Python version' do |requested_version, resolved_version|
it "builds with Python #{requested_version}" do
app.deploy do |app|
if requested_version == '3.13'
expect(clean_output(app.output)).to include(<<~OUTPUT)
remote: -----> Python app detected
remote: -----> Using Python #{requested_version} specified in .python-version
remote: -----> Installing Python #{resolved_version}
remote: -----> Installing pip #{PIP_VERSION}
remote: -----> Installing dependencies using 'pip install -r requirements.txt'
remote: Collecting typing-extensions==4.12.2 (from -r requirements.txt (line 2))
OUTPUT
else
expect(clean_output(app.output)).to include(<<~OUTPUT)
remote: -----> Python app detected
remote: -----> Using Python #{requested_version} specified in .python-version
remote: -----> Installing Python #{resolved_version}
remote: -----> Installing pip #{PIP_VERSION}, setuptools #{SETUPTOOLS_VERSION} and wheel #{WHEEL_VERSION}
remote: -----> Installing SQLite3
remote: -----> Installing dependencies using 'pip install -r requirements.txt'
remote: Collecting typing-extensions==4.12.2 (from -r requirements.txt (line 2))
OUTPUT
end
expect(app.run('python -V')).to include("Python #{resolved_version}")
end
end
end
RSpec.describe 'Python version support' do
context 'when no Python version is specified' do
let(:buildpacks) { [:default] }
let(:app) { Hatchet::Runner.new('spec/fixtures/python_version_unspecified', buildpacks:) }
context 'with a new app' do
it 'builds with the default Python version' do
app.deploy do |app|
expect(clean_output(app.output)).to include(<<~OUTPUT)
remote: -----> Python app detected
remote: -----> No Python version was specified. Using the buildpack default: Python #{DEFAULT_PYTHON_MAJOR_VERSION}
remote: To use a different version, see: https://devcenter.heroku.com/articles/python-runtimes
remote: -----> Installing Python #{DEFAULT_PYTHON_FULL_VERSION}
OUTPUT
end
end
end
context 'with an app last built using an older default Python version' do
# This test performs an initial build using an older buildpack version, followed
# by a build using the current version. This ensures that the current buildpack
# can successfully read the version metadata written to the build cache in the past.
let(:buildpacks) { ['https://github.com/heroku/heroku-buildpack-python#v257'] }
it 'builds with the same Python version as the last build' do
app.deploy do |app|
update_buildpacks(app, [:default])
app.commit!
app.push!
expect(clean_output(app.output)).to include(<<~OUTPUT)
remote: -----> Python app detected
remote: -----> No Python version was specified. Using the same major version as the last build: Python 3.12
remote: To use a different version, see: https://devcenter.heroku.com/articles/python-runtimes
remote: -----> Discarding cache since:
remote: - The Python version has changed from 3.12.6 to #{LATEST_PYTHON_3_12}
remote: - The pip version has changed from 24.0 to #{PIP_VERSION}
remote: -----> Installing Python #{LATEST_PYTHON_3_12}
remote: -----> Installing pip #{PIP_VERSION}, setuptools #{SETUPTOOLS_VERSION} and wheel #{WHEEL_VERSION}
OUTPUT
expect(app.run('python -V')).to include("Python #{LATEST_PYTHON_3_12}")
end
end
end
end
context 'when .python-version contains Python 3.8' do
let(:allow_failure) { false }
let(:app) { Hatchet::Runner.new('spec/fixtures/python_3.8', allow_failure:) }
context 'when using Heroku-20', stacks: %w[heroku-20] do
it 'builds with latest Python 3.8 but shows a deprecation warning' do
app.deploy do |app|
expect(clean_output(app.output)).to include(<<~OUTPUT)
remote: -----> Python app detected
remote: -----> Using Python 3.8 specified in .python-version
remote: -----> Installing Python #{LATEST_PYTHON_3_8}
remote:
remote: ! Warning: Support for Python 3.8 is ending soon!
remote: !
remote: ! Python 3.8 will reach its upstream end-of-life in October 2024, at which
remote: ! point it will no longer receive security updates:
remote: ! https://devguide.python.org/versions/#supported-versions
remote: !
remote: ! Support for Python 3.8 will be removed from this buildpack on December 4th, 2024.
remote: !
remote: ! Upgrade to a newer Python version as soon as possible to keep your app secure.
remote: ! See: https://devcenter.heroku.com/articles/python-runtimes
remote:
remote: -----> Installing pip #{PIP_VERSION}, setuptools #{SETUPTOOLS_VERSION} and wheel #{WHEEL_VERSION}
remote: -----> Installing SQLite3
remote: -----> Installing dependencies using 'pip install -r requirements.txt'
remote: Collecting typing-extensions==4.12.2 (from -r requirements.txt (line 2))
OUTPUT
expect(app.run('python -V')).to include("Python #{LATEST_PYTHON_3_8}")
end
end
end
context 'when using Heroku-22 or newer', stacks: %w[heroku-22 heroku-24] do
let(:allow_failure) { true }
# We only support Python 3.8 on Heroku-20 and older.
it 'aborts the build with a version not available message' do
app.deploy do |app|
expect(clean_output(app.output)).to include(<<~OUTPUT)
remote: -----> Python app detected
remote: -----> Using Python 3.8 specified in .python-version
remote:
remote: ! Error: Python #{LATEST_PYTHON_3_8} isn't available for this stack (#{app.stack}).
remote: !
remote: ! For a list of the supported Python versions, see:
remote: ! https://devcenter.heroku.com/articles/python-support#supported-runtimes
remote:
remote: ! Push rejected, failed to compile Python app.
OUTPUT
end
end
end
end
context 'when .python-version contains Python 3.9' do
let(:app) { Hatchet::Runner.new('spec/fixtures/python_3.9') }
include_examples 'builds with the requested Python version', '3.9', LATEST_PYTHON_3_9
end
context 'when .python-version contains Python 3.10' do
let(:app) { Hatchet::Runner.new('spec/fixtures/python_3.10') }
include_examples 'builds with the requested Python version', '3.10', LATEST_PYTHON_3_10
end
context 'when .python-version contains Python 3.11' do
let(:app) { Hatchet::Runner.new('spec/fixtures/python_3.11') }
include_examples 'builds with the requested Python version', '3.11', LATEST_PYTHON_3_11
end
context 'when .python-version contains Python 3.12' do
let(:app) { Hatchet::Runner.new('spec/fixtures/python_3.12') }
include_examples 'builds with the requested Python version', '3.12', LATEST_PYTHON_3_12
end
context 'when .python-version contains Python 3.13' do
let(:app) { Hatchet::Runner.new('spec/fixtures/python_3.13') }
include_examples 'builds with the requested Python version', '3.13', LATEST_PYTHON_3_13
end
context 'when .python-version contains an invalid Python version string' do
let(:app) { Hatchet::Runner.new('spec/fixtures/python_version_file_invalid_version', allow_failure: true) }
it 'aborts the build with an invalid .python-version message' do
app.deploy do |app|
expect(clean_output(app.output)).to include(<<~OUTPUT)
remote: -----> Python app detected
remote:
remote: ! Error: Invalid Python version in .python-version.
remote: !
remote: ! The Python version specified in '.python-version' isn't in
remote: ! the correct format.
remote: !
remote: ! The following version was found:
remote: ! 3.12.0invalid
remote: !
remote: ! However, the version must be specified as either:
remote: ! 1. '<major>.<minor>' (recommended, for automatic patch updates)
remote: ! 2. '<major>.<minor>.<patch>' (to pin to an exact patch version)
remote: !
remote: ! Don't include quotes or a 'python-' prefix. To include
remote: ! comments, add them on their own line, prefixed with '#'.
remote: !
remote: ! For example, to request the latest version of Python #{DEFAULT_PYTHON_MAJOR_VERSION},
remote: ! update the '.python-version' file so it contains:
remote: ! #{DEFAULT_PYTHON_MAJOR_VERSION}
remote:
remote: ! Push rejected, failed to compile Python app.
OUTPUT
end
end
end
context 'when .python-version does not contain a Python version' do
let(:app) { Hatchet::Runner.new('spec/fixtures/python_version_file_no_version', allow_failure: true) }
it 'aborts the build with a no version string found message' do
app.deploy do |app|
expect(clean_output(app.output)).to include(<<~OUTPUT)
remote: -----> Python app detected
remote:
remote: ! Error: Invalid Python version in .python-version.
remote: !
remote: ! No Python version was found in the '.python-version' file.
remote: !
remote: ! Update the file so that it contains a valid Python version
remote: ! such as '#{DEFAULT_PYTHON_MAJOR_VERSION}'.
remote: !
remote: ! If the file already contains a version, check the line doesn't
remote: ! begin with a '#', otherwise it will be treated as a comment.
remote:
remote: ! Push rejected, failed to compile Python app.
OUTPUT
end
end
end
context 'when .python-version contains multiple Python versions' do
let(:app) { Hatchet::Runner.new('spec/fixtures/python_version_file_multiple_versions', allow_failure: true) }
it 'aborts the build with a multiple versions not supported message' do
app.deploy do |app|
expect(clean_output(app.output)).to include(<<~OUTPUT)
remote: -----> Python app detected
remote:
remote: ! Error: Invalid Python version in .python-version.
remote: !
remote: ! Multiple Python versions were found in the '.python-version'
remote: ! file:
remote: !
remote: ! // invalid comment
remote: ! 3.12
remote: ! 2.7
remote: !
remote: ! Update the file so it contains only one Python version.
remote: !
remote: ! If the additional versions are actually comments, prefix
remote: ! those lines with '#'.
remote:
remote: ! Push rejected, failed to compile Python app.
OUTPUT
end
end
end
context 'when .python-version contains an EOL Python 3.x version' do
let(:app) { Hatchet::Runner.new('spec/fixtures/python_version_eol', allow_failure: true) }
it 'aborts the build with an EOL message' do
app.deploy do |app|
expect(clean_output(app.output)).to include(<<~OUTPUT)
remote: -----> Python app detected
remote: -----> Using Python 3.7 specified in .python-version
remote:
remote: ! Error: The requested Python version has reached end-of-life.
remote: !
remote: ! Python 3.7 has reached its upstream end-of-life, and is
remote: ! therefore no longer receiving security updates:
remote: ! https://devguide.python.org/versions/#supported-versions
remote: !
remote: ! As such, it is no longer supported by this buildpack.
remote: !
remote: ! Please upgrade to a newer Python version by updating the
remote: ! version configured via the '.python-version' file.
remote: !
remote: ! For a list of the supported Python versions, see:
remote: ! https://devcenter.heroku.com/articles/python-support#supported-runtimes
remote:
remote: ! Push rejected, failed to compile Python app.
OUTPUT
end
end
end
context 'when .python-version contains an non-existent Python major version' do
let(:app) { Hatchet::Runner.new('spec/fixtures/python_version_non_existent_major', allow_failure: true) }
it 'aborts the build with an invalid .python-version message' do
app.deploy do |app|
expect(clean_output(app.output)).to include(<<~OUTPUT)
remote: -----> Python app detected
remote: -----> Using Python 3.999 specified in .python-version
remote:
remote: ! Error: The requested Python version isn't recognised.
remote: !
remote: ! The requested Python version 3.999 isn't recognised.
remote: !
remote: ! Check that this Python version has been officially released,
remote: ! and that the Python buildpack has added support for it:
remote: ! https://devguide.python.org/versions/#supported-versions
remote: ! https://devcenter.heroku.com/articles/python-support#supported-runtimes
remote: !
remote: ! If it has, make sure that you are using the latest version
remote: ! of this buildpack:
remote: ! https://devcenter.heroku.com/articles/python-support#checking-the-python-buildpack-version
remote: !
remote: ! Otherwise, switch to a supported version (such as Python #{DEFAULT_PYTHON_MAJOR_VERSION})
remote: ! by updating the version configured via the '.python-version' file.
remote:
remote: ! Push rejected, failed to compile Python app.
OUTPUT
end
end
end
context 'when .python-version contains a non-existent Python patch version' do
let(:app) { Hatchet::Runner.new('spec/fixtures/python_version_non_existent_patch', allow_failure: true) }
it 'aborts the build with a version not available message' do
app.deploy do |app|
expect(clean_output(app.output)).to include(<<~OUTPUT)
remote: -----> Python app detected
remote: -----> Using Python 3.12.999 specified in .python-version
remote:
remote: ! Error: Python 3.12.999 isn't available for this stack (#{app.stack}).
remote: !
remote: ! For a list of the supported Python versions, see:
remote: ! https://devcenter.heroku.com/articles/python-support#supported-runtimes
remote:
remote: ! Push rejected, failed to compile Python app.
OUTPUT
end
end
end
context 'when runtime.txt contains an invalid Python version string' do
let(:app) { Hatchet::Runner.new('spec/fixtures/runtime_txt_invalid_version', allow_failure: true) }
it 'aborts the build with an invalid runtime.txt message' do
app.deploy do |app|
expect(clean_output(app.output)).to include(<<~OUTPUT)
remote: -----> Python app detected
remote:
remote: ! Error: Invalid Python version in runtime.txt.
remote: !
remote: ! The Python version specified in 'runtime.txt' isn't in
remote: ! the correct format.
remote: !
remote: ! The following file contents were found:
remote: ! python-3.12.0invalid
remote: !
remote: ! However, the version must be specified as either:
remote: ! 1. 'python-<major>.<minor>' (recommended, for automatic patch updates)
remote: ! 2. 'python-<major>.<minor>.<patch>' (to pin to an exact patch version)
remote: !
remote: ! Remember to include the 'python-' prefix. Comments aren't supported.
remote: !
remote: ! For example, to request the latest version of Python #{DEFAULT_PYTHON_MAJOR_VERSION},
remote: ! update the 'runtime.txt' file so it contains:
remote: ! python-#{DEFAULT_PYTHON_MAJOR_VERSION}
remote:
remote: ! Push rejected, failed to compile Python app.
OUTPUT
end
end
end
context 'when runtime.txt contains an EOL Python 2.x version' do
let(:app) { Hatchet::Runner.new('spec/fixtures/runtime_txt_eol_version', allow_failure: true) }
it 'aborts the build with an EOL message' do
app.deploy do |app|
expect(clean_output(app.output)).to include(<<~OUTPUT)
remote: -----> Python app detected
remote: -----> Using Python 2.7.18 specified in runtime.txt
remote:
remote: ! Error: The requested Python version has reached end-of-life.
remote: !
remote: ! Python 2.7 has reached its upstream end-of-life, and is
remote: ! therefore no longer receiving security updates:
remote: ! https://devguide.python.org/versions/#supported-versions
remote: !
remote: ! As such, it is no longer supported by this buildpack.
remote: !
remote: ! Please upgrade to a newer Python version by updating the
remote: ! version configured via the 'runtime.txt' file.
remote: !
remote: ! For a list of the supported Python versions, see:
remote: ! https://devcenter.heroku.com/articles/python-support#supported-runtimes
remote:
remote: ! Push rejected, failed to compile Python app.
OUTPUT
end
end
end
# This also tests runtime.txt support for the major version only syntax, as well as the handling
# of runtime.txt files that contain stray whitespace.
context 'when there is both a runtime.txt and .python-version file' do
let(:app) { Hatchet::Runner.new('spec/fixtures/runtime_txt_and_python_version_file') }
it 'builds with the version from runtime.txt' do
app.deploy do |app|
expect(clean_output(app.output)).to include(<<~OUTPUT)
remote: -----> Python app detected
remote: -----> Using Python 3.13 specified in runtime.txt
remote: -----> Installing Python #{LATEST_PYTHON_3_13}
remote: -----> Installing pip #{PIP_VERSION}
remote: -----> Installing dependencies using 'pip install -r requirements.txt'
OUTPUT
end
end
end
context 'when the requested Python version has changed since the last build' do
let(:app) { Hatchet::Runner.new('spec/fixtures/python_3.9') }
it 'builds with the new Python version after removing the old install' do
app.deploy do |app|
File.write('.python-version', '3.13')
app.commit!
app.push!
expect(clean_output(app.output)).to include(<<~OUTPUT)
remote: -----> Python app detected
remote: -----> Using Python 3.13 specified in .python-version
remote: -----> Discarding cache since:
remote: - The Python version has changed from #{LATEST_PYTHON_3_9} to #{LATEST_PYTHON_3_13}
remote: -----> Installing Python #{LATEST_PYTHON_3_13}
remote: -----> Installing pip #{PIP_VERSION}
remote: -----> Installing dependencies using 'pip install -r requirements.txt'
remote: Collecting typing-extensions==4.12.2 (from -r requirements.txt (line 2))
OUTPUT
end
end
end
end