[ruby] Show error when Ruby 2.5.x detected (#7126)

* [ruby] Show error when Ruby 2.5.x detected

* Add test with ruby 2.5.x
This commit is contained in:
Steven
2021-12-03 14:34:01 -05:00
committed by GitHub
parent 34f4222ca2
commit b94337d842
9 changed files with 86 additions and 6 deletions

View File

@@ -9,13 +9,24 @@ interface RubyVersion extends NodeVersion {
const allOptions: RubyVersion[] = [ const allOptions: RubyVersion[] = [
{ major: 2, minor: 7, range: '2.7.x', runtime: 'ruby2.7' }, { major: 2, minor: 7, range: '2.7.x', runtime: 'ruby2.7' },
{ major: 2, minor: 5, range: '2.5.x', runtime: 'ruby2.5' }, {
major: 2,
minor: 5,
range: '2.5.x',
runtime: 'ruby2.5',
discontinueDate: new Date('2021-11-30'),
},
]; ];
function getLatestRubyVersion(): RubyVersion { function getLatestRubyVersion(): RubyVersion {
return allOptions[0]; return allOptions[0];
} }
function isDiscontinued({ discontinueDate }: RubyVersion): boolean {
const today = Date.now();
return discontinueDate !== undefined && discontinueDate.getTime() <= today;
}
function getRubyPath(meta: Meta, gemfileContents: string) { function getRubyPath(meta: Meta, gemfileContents: string) {
let selection = getLatestRubyVersion(); let selection = getLatestRubyVersion();
if (meta.isDev) { if (meta.isDev) {
@@ -37,8 +48,20 @@ function getRubyPath(meta: Meta, gemfileContents: string) {
if (!found) { if (!found) {
throw new NowBuildError({ throw new NowBuildError({
code: 'RUBY_INVALID_VERSION', code: 'RUBY_INVALID_VERSION',
message: 'Found `Gemfile` with invalid Ruby version: `' + line + '`.', message: `Found \`Gemfile\` with invalid Ruby version: \`${line}.\``,
link: 'https://vercel.com/docs/runtimes#official-runtimes/ruby/ruby-version', link: 'http://vercel.link/ruby-version',
});
}
if (isDiscontinued(selection)) {
const latest = getLatestRubyVersion();
const intro = `Found \`Gemfile\` with discontinued Ruby version: \`${line}.\``;
const hint = `Please set \`ruby "~> ${latest.range}"\` in your \`Gemfile\` to use Ruby ${latest.range}.`;
const upstream =
'This change is the result of a decision made by an upstream infrastructure provider (AWS).';
throw new NowBuildError({
code: 'RUBY_DISCONTINUED_VERSION',
link: 'http://vercel.link/ruby-version',
message: `${intro} ${hint} ${upstream}`,
}); });
} }
} }

View File

@@ -1,6 +1,5 @@
{ {
"version": 2, "version": 2,
"builds": [{ "src": "index.rb", "use": "@vercel/ruby" }], "builds": [{ "src": "index.rb", "use": "@vercel/ruby" }],
"build": { "env": { "RUBY_VERSION": "2.7.x" } },
"probes": [{ "path": "/", "mustContain": "gem:RANDOMNESS_PLACEHOLDER" }] "probes": [{ "path": "/", "mustContain": "gem:RANDOMNESS_PLACEHOLDER" }]
} }

View File

@@ -1,7 +1,6 @@
{ {
"version": 2, "version": 2,
"builds": [{ "src": "project/index.rb", "use": "@vercel/ruby" }], "builds": [{ "src": "project/index.rb", "use": "@vercel/ruby" }],
"build": { "env": { "RUBY_VERSION": "2.7.x" } },
"probes": [ "probes": [
{ "path": "/project/", "mustContain": "gem:RANDOMNESS_PLACEHOLDER" } { "path": "/project/", "mustContain": "gem:RANDOMNESS_PLACEHOLDER" }
] ]

View File

@@ -1,6 +1,5 @@
{ {
"version": 2, "version": 2,
"builds": [{ "src": "index.ru", "use": "@vercel/ruby" }], "builds": [{ "src": "index.ru", "use": "@vercel/ruby" }],
"build": { "env": { "RUBY_VERSION": "2.7.x" } },
"probes": [{ "path": "/", "mustContain": "gem:RANDOMNESS_PLACEHOLDER" }] "probes": [{ "path": "/", "mustContain": "gem:RANDOMNESS_PLACEHOLDER" }]
} }

View File

@@ -0,0 +1,7 @@
# frozen_string_literal: true
source "https://rubygems.org"
ruby "~> 2.5.x"
gem "cowsay", "~> 0.3.0"

View File

@@ -0,0 +1,16 @@
GEM
remote: https://rubygems.org/
specs:
cowsay (0.3.0)
PLATFORMS
x86_64-linux
DEPENDENCIES
cowsay (~> 0.3.0)
RUBY VERSION
ruby 2.5.5p157
BUNDLED WITH
2.2.22

View File

@@ -0,0 +1,9 @@
require 'cowsay'
Handler = Proc.new do |req, res|
name = req.query['name'] || 'World'
res.status = 200
res['Content-Type'] = 'text/text; charset=utf-8'
res.body = Cowsay.say("Hello #{name}", 'cow')
end

View File

@@ -0,0 +1,4 @@
{
"version": 2,
"builds": [{ "src": "index.rb", "use": "@vercel/ruby" }]
}

View File

@@ -23,8 +23,32 @@ beforeAll(async () => {
const fixturesPath = path.resolve(__dirname, 'fixtures'); const fixturesPath = path.resolve(__dirname, 'fixtures');
const testsThatFailToBuild = new Map([
[
'11-version-2-5-error',
'Found `Gemfile` with discontinued Ruby version: `ruby "~> 2.5.x".` Please set `ruby "~> 2.7.x"` in your `Gemfile` to use Ruby 2.7.x. This change is the result of a decision made by an upstream infrastructure provider (AWS).',
],
]);
// eslint-disable-next-line no-restricted-syntax // eslint-disable-next-line no-restricted-syntax
for (const fixture of fs.readdirSync(fixturesPath)) { for (const fixture of fs.readdirSync(fixturesPath)) {
const errMsg = testsThatFailToBuild.get(fixture);
if (errMsg) {
// eslint-disable-next-line no-loop-func
it(`should fail to build ${fixture}`, async () => {
try {
await testDeployment(
{ builderUrl, buildUtilsUrl },
path.join(fixturesPath, fixture)
);
} catch (err) {
expect(err).toBeTruthy();
expect(err.deployment).toBeTruthy();
expect(err.deployment.errorMessage).toBe(errMsg);
}
});
continue; //eslint-disable-line
}
// eslint-disable-next-line no-loop-func // eslint-disable-next-line no-loop-func
it(`should build ${fixture}`, async () => { it(`should build ${fixture}`, async () => {
await expect( await expect(