Skip to content
Snippets Groups Projects
Select Git revision
  • a446c2bc6d67e6337fe56e556194252156afbdb0
  • master default
2 results

index.spec.ts

Blame
  • index.spec.ts 5.60 KiB
    import { Fixtures } from '../../../../test/fixtures';
    import * as httpMock from '../../../../test/http-mock';
    import { mocked } from '../../../../test/util';
    import * as _hostRules from '../../../util/host-rules';
    import { GoDatasource } from '.';
    
    jest.mock('../../../util/host-rules');
    const hostRules = mocked(_hostRules);
    
    const getReleasesDirectMock = jest.fn();
    
    const getDigestGithubMock = jest.fn();
    const getDigestGitlabMock = jest.fn();
    const getDigestGitMock = jest.fn();
    const getDigestBitbucketMock = jest.fn();
    jest.mock('./releases-direct', () => {
      return {
        GoDirectDatasource: jest.fn().mockImplementation(() => {
          return {
            git: { getDigest: () => getDigestGitMock() },
            github: { getDigest: () => getDigestGithubMock() },
            gitlab: { getDigest: () => getDigestGitlabMock() },
            bitbucket: { getDigest: () => getDigestBitbucketMock() },
            getReleases: () => getReleasesDirectMock(),
          };
        }),
      };
    });
    
    const getReleasesProxyMock = jest.fn();
    jest.mock('./releases-goproxy', () => {
      return {
        GoProxyDatasource: jest.fn().mockImplementation(() => {
          return {
            getReleases: () => getReleasesProxyMock(),
          };
        }),
      };
    });
    
    const datasource = new GoDatasource();
    
    describe('modules/datasource/go/index', () => {
      describe('getReleases', () => {
        beforeEach(() => {
          jest.resetAllMocks();
          hostRules.find.mockReturnValue({});
          hostRules.hosts.mockReturnValue([]);
        });
    
        afterEach(() => {
          jest.resetAllMocks();
          delete process.env.GOPROXY;
        });
    
        it('fetches release info directly from VCS', async () => {
          const expected = { releases: [{ version: '0.0.1' }] };
          getReleasesProxyMock.mockResolvedValue(null);
          getReleasesDirectMock.mockResolvedValue(expected);
    
          const res = await datasource.getReleases({
            packageName: 'golang.org/foo/bar',
          });
    
          expect(res).toBe(expected);
          expect(getReleasesProxyMock).not.toHaveBeenCalled();
          expect(getReleasesDirectMock).toHaveBeenCalled();
        });
    
        it('supports GOPROXY', async () => {
          const expected = { releases: [{ version: '0.0.1' }] };
          getReleasesProxyMock.mockResolvedValue(expected);
          getReleasesDirectMock.mockResolvedValue(null);
          process.env.GOPROXY = 'https://proxy.golang.org,direct';
    
          const res = await datasource.getReleases({
            packageName: 'golang.org/foo/bar',
          });
    
          expect(res).toBe(expected);
          expect(getReleasesProxyMock).toHaveBeenCalled();
          expect(getReleasesDirectMock).not.toHaveBeenCalled();
        });
      });
    
      describe('getDigest', () => {
        beforeEach(() => {
          jest.resetAllMocks();
          hostRules.find.mockReturnValue({});
          hostRules.hosts.mockReturnValue([]);
        });
    
        afterEach(() => {
          jest.resetAllMocks();
        });
    
        it('returns null for no go-source tag', async () => {
          httpMock
            .scope('https://golang.org/')
            .get('/y/text?go-get=1')
            .reply(200, '');
          const res = await datasource.getDigest(
            { packageName: 'golang.org/y/text' },
            null
          );
          expect(res).toBeNull();
        });
    
        it('returns null for wrong name', async () => {
          httpMock
            .scope('https://golang.org/')
            .get('/y/text?go-get=1')
            .reply(200, Fixtures.get('go-get-github.html'));
          const res = await datasource.getDigest(
            { packageName: 'golang.org/y/text' },
            null
          );
          expect(res).toBeNull();
        });
    
        it('supports gitlab digest', async () => {
          httpMock
            .scope('https://gitlab.com/')
            .get('/group/subgroup?go-get=1')
            .reply(200, Fixtures.get('go-get-gitlab.html'));
          getDigestGitlabMock.mockResolvedValue('abcdefabcdefabcdefabcdef');
          const res = await datasource.getDigest(
            { packageName: 'gitlab.com/group/subgroup' },
            null
          );
          expect(res).toBe('abcdefabcdefabcdefabcdef');
        });
    
        it('supports git digest', async () => {
          httpMock
            .scope('https://renovatebot.com/')
            .get('/abc/def?go-get=1')
            .reply(200, Fixtures.get('go-get-git-digest.html'));
          getDigestGitMock.mockResolvedValue('abcdefabcdefabcdefabcdef');
          const res = await datasource.getDigest(
            { packageName: 'renovatebot.com/abc/def' },
            null
          );
          expect(res).toBe('abcdefabcdefabcdefabcdef');
        });
    
        it('supports gitlab digest with a specific branch', async () => {
          const branch = 'some-branch';
          httpMock
            .scope('https://gitlab.com/')
            .get('/group/subgroup?go-get=1')
            .reply(200, Fixtures.get('go-get-gitlab.html'));
          getDigestGitlabMock.mockResolvedValue('abcdefabcdefabcdefabcdef');
          const res = await datasource.getDigest(
            { packageName: 'gitlab.com/group/subgroup' },
            branch
          );
          expect(res).toBe('abcdefabcdefabcdefabcdef');
        });
    
        it('returns digest', async () => {
          httpMock
            .scope('https://golang.org/')
            .get('/x/text?go-get=1')
            .reply(200, Fixtures.get('go-get-github.html'));
          getDigestGithubMock.mockResolvedValueOnce('abcdefabcdefabcdefabcdef');
          const res = await datasource.getDigest(
            { packageName: 'golang.org/x/text' },
            null
          );
          expect(res).toBe('abcdefabcdefabcdefabcdef');
        });
    
        it('support bitbucket digest', async () => {
          getDigestBitbucketMock.mockResolvedValueOnce('123');
          const res = await datasource.getDigest(
            {
              packageName: 'bitbucket.org/golang/text',
            },
            null
          );
          expect(res).toMatchSnapshot();
          expect(res).not.toBeNull();
          expect(res).toBeDefined();
        });
      });
    });