From caea7597f9f6560c48854904be82c0999dcba7bd Mon Sep 17 00:00:00 2001 From: Marat Reymers <16486128+maratori@users.noreply.github.com> Date: Sun, 7 Apr 2024 10:02:49 +0200 Subject: [PATCH] [GitHubGoMod] Ignore comment after version (fixes #10079) (#10080) * [GithubGoMod] Ignore comment after version (fixes #10079) * add unit tests for GithubGoModGoVersion.transform --------- Co-authored-by: chris48s --- services/github/github-go-mod.service.js | 2 +- services/github/github-go-mod.spec.js | 26 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 services/github/github-go-mod.spec.js diff --git a/services/github/github-go-mod.service.js b/services/github/github-go-mod.service.js index ed678e38f9f29..77f2080b52492 100644 --- a/services/github/github-go-mod.service.js +++ b/services/github/github-go-mod.service.js @@ -9,7 +9,7 @@ const queryParamSchema = Joi.object({ filename: Joi.string(), }).required() -const goVersionRegExp = /^go (.+)$/m +const goVersionRegExp = /^go ([^/\s]+)(\s*\/.+)?$/m const filenameDescription = 'The `filename` param can be used to specify the path to `go.mod`. By default, we look for `go.mod` in the repo root' diff --git a/services/github/github-go-mod.spec.js b/services/github/github-go-mod.spec.js new file mode 100644 index 0000000000000..cec2276509d80 --- /dev/null +++ b/services/github/github-go-mod.spec.js @@ -0,0 +1,26 @@ +import { expect } from 'chai' +import { test, given } from 'sazerac' +import { InvalidResponse } from '../index.js' +import GithubGoModGoVersion from './github-go-mod.service.js' + +describe('GithubGoModGoVersion', function () { + describe('valid cases', function () { + test(GithubGoModGoVersion.transform, () => { + given('go 1.18').expect({ go: '1.18' }) + given('go 1.18 // inline comment').expect({ go: '1.18' }) + given('go 1.18// inline comment').expect({ go: '1.18' }) + given('go 1.18 /* block comment */').expect({ go: '1.18' }) + given('go 1.18/* block comment */').expect({ go: '1.18' }) + given('go 1').expect({ go: '1' }) + given('go 1.2.3').expect({ go: '1.2.3' }) + given('go string').expect({ go: 'string' }) + }) + }) + + describe('invalid cases', function () { + expect(() => GithubGoModGoVersion.transform('')).to.throw(InvalidResponse) + expect(() => + GithubGoModGoVersion.transform("doesn't start with go"), + ).to.throw(InvalidResponse) + }) +})