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) + }) +})