Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(go): Mimic Go logic for GOPRIVATE parsing #13166

Merged
merged 1 commit into from
Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
63 changes: 51 additions & 12 deletions lib/datasource/go/releases-goproxy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,31 @@ describe('datasource/go/releases-goproxy', () => {
expect(parseNoproxy(undefined)).toBeNull();
expect(parseNoproxy(null)).toBeNull();
expect(parseNoproxy('')).toBeNull();
expect(parseNoproxy('*')?.source).toBe('^(?:[^\\/]*)$');
expect(parseNoproxy('?')?.source).toBe('^(?:[^\\/])$');
expect(parseNoproxy('foo')?.source).toBe('^(?:foo)$');
expect(parseNoproxy('\\f\\o\\o')?.source).toBe('^(?:foo)$');
expect(parseNoproxy('foo,bar')?.source).toBe('^(?:foo|bar)$');
expect(parseNoproxy('[abc]')?.source).toBe('^(?:[abc])$');
expect(parseNoproxy('[a-c]')?.source).toBe('^(?:[a-c])$');
expect(parseNoproxy('[\\a-\\c]')?.source).toBe('^(?:[a-c])$');
expect(parseNoproxy('a.b.c')?.source).toBe('^(?:a\\.b\\.c)$');
expect(parseNoproxy('/')).toBeNull();
expect(parseNoproxy('*')?.source).toBe('^(?:[^\\/]*)(?:\\/.*)?$');
expect(parseNoproxy('?')?.source).toBe('^(?:[^\\/])(?:\\/.*)?$');
expect(parseNoproxy('foo')?.source).toBe('^(?:foo)(?:\\/.*)?$');
expect(parseNoproxy('\\f\\o\\o')?.source).toBe('^(?:foo)(?:\\/.*)?$');
expect(parseNoproxy('foo,bar')?.source).toBe('^(?:foo|bar)(?:\\/.*)?$');
expect(parseNoproxy('[abc]')?.source).toBe('^(?:[abc])(?:\\/.*)?$');
expect(parseNoproxy('[a-c]')?.source).toBe('^(?:[a-c])(?:\\/.*)?$');
expect(parseNoproxy('[\\a-\\c]')?.source).toBe('^(?:[a-c])(?:\\/.*)?$');
expect(parseNoproxy('a.b.c')?.source).toBe('^(?:a\\.b\\.c)(?:\\/.*)?$');
expect(parseNoproxy('trailing/')?.source).toBe(
'^(?:trailing)(?:\\/.*)?$'
);
});

it('matches on real package prefixes', () => {
expect(parseNoproxy('ex.co').test('ex.co/foo')).toBeTrue();
expect(parseNoproxy('ex.co/').test('ex.co/foo')).toBeTrue();
expect(parseNoproxy('ex.co/foo/bar').test('ex.co/foo/bar')).toBeTrue();
expect(parseNoproxy('ex.co/foo/bar').test('ex.co/foo/bar')).toBeTrue();
expect(parseNoproxy('*/foo/*').test('example.com/foo/bar')).toBeTrue();
expect(parseNoproxy('ex.co/foo/*').test('ex.co/foo/bar')).toBeTrue();
expect(parseNoproxy('ex.co/foo/*').test('ex.co/foo/baz')).toBeTrue();
expect(parseNoproxy('ex.co').test('ex.co/foo/v2')).toBeTrue();

expect(
parseNoproxy('ex.co/foo/bar,ex.co/foo/baz').test('ex.co/foo/bar')
).toBeTrue();
Expand All @@ -123,15 +132,45 @@ describe('datasource/go/releases-goproxy', () => {
expect(
parseNoproxy('ex.co/foo/bar,ex.co/foo/baz').test('ex.co/foo/qux')
).toBeFalse();
});

it('Matches from start to end', () => {
expect(parseNoproxy('x').test('x/aba')).toBeFalse();
expect(parseNoproxy('ex').test('ex.co/foo')).toBeFalse();

expect(parseNoproxy('aba').test('x/aba')).toBeFalse();
expect(parseNoproxy('x/b').test('x/aba')).toBeFalse();
expect(parseNoproxy('x/ab').test('x/aba')).toBeFalse();
expect(parseNoproxy('x/ab[a-b]').test('x/aba')).toBeTrue();
});

it('matches on wildcards', () => {
expect(parseNoproxy('/*/').test('ex.co/foo')).toBeFalse();
expect(parseNoproxy('*/foo').test('ex.co/foo')).toBeTrue();
expect(parseNoproxy('*/fo').test('ex.co/foo')).toBeFalse();
expect(parseNoproxy('*/fo?').test('ex.co/foo')).toBeTrue();
expect(parseNoproxy('*/fo*').test('ex.co/foo')).toBeTrue();
expect(parseNoproxy('*fo*').test('ex.co/foo')).toBeFalse();

expect(parseNoproxy('*.co').test('ex.co/foo')).toBeTrue();
expect(parseNoproxy('ex*').test('ex.co/foo')).toBeTrue();
expect(parseNoproxy('*/foo').test('ex.co/foo/v2')).toBeTrue();
expect(parseNoproxy('*/foo/').test('ex.co/foo/v2')).toBeTrue();
expect(parseNoproxy('*/foo/*').test('ex.co/foo/v2')).toBeTrue();
expect(parseNoproxy('*/foo/*/').test('ex.co/foo/v2')).toBeTrue();
expect(parseNoproxy('*/v2').test('ex.co/foo/v2')).toBeFalse();
expect(parseNoproxy('*/*/v2').test('ex.co/foo/v2')).toBeTrue();
expect(parseNoproxy('*/*/*').test('ex.co/foo/v2')).toBeTrue();
expect(parseNoproxy('*/*/*/').test('ex.co/foo/v2')).toBeTrue();
expect(parseNoproxy('*/*/*').test('ex.co/foo')).toBeFalse();
expect(parseNoproxy('*/*/*/').test('ex.co/foo')).toBeFalse();

expect(parseNoproxy('*/*/*,,').test('ex.co/repo')).toBeFalse();
expect(parseNoproxy('*/*/*,,*/repo').test('ex.co/repo')).toBeTrue();
expect(parseNoproxy(',,*/repo').test('ex.co/repo')).toBeTrue();
});

it('matches on character ranges', () => {
expect(parseNoproxy('x/ab[a-b]').test('x/aba')).toBeTrue();
expect(parseNoproxy('x/ab[a-b]').test('x/abc')).toBeFalse();
});
});

describe('getReleases', () => {
Expand Down
8 changes: 7 additions & 1 deletion lib/datasource/go/releases-goproxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ const lexer = moo.states({
push: 'characterRange',
value: (_: string) => '[',
},
trailingSlash: {
rarkins marked this conversation as resolved.
Show resolved Hide resolved
match: /\/$/,
value: (_: string) => '',
},
char: {
match: /[^*?\\[\n]/,
value: (s: string) => s.replace(regEx('\\.', 'g'), '\\.'),
Expand Down Expand Up @@ -107,7 +111,9 @@ export function parseNoproxy(
}
lexer.reset(input);
const noproxyPattern = [...lexer].map(({ value }) => value).join('');
const result = noproxyPattern ? regEx(`^(?:${noproxyPattern})$`) : null;
const result = noproxyPattern
? regEx(`^(?:${noproxyPattern})(?:/.*)?$`)
: null;
parsedNoproxy[input] = result;
return result;
}
Expand Down