Skip to content
Snippets Groups Projects
Select Git revision
  • 1fe7ab7b68b07b35e752d7273aff2aaa3145f39c
  • main default protected
  • next
  • chore/update-static-data
  • renovate/main-redis-5.x
  • feat/gnupg
  • fix/36615b-branch-reuse-no-cache
  • chore/punycode
  • refactor/pin-new-value
  • feat/36219--git-x509-signing
  • feat/structured-logger
  • hotfix/39.264.1
  • feat/skip-dangling
  • gh-readonly-queue/next/pr-36034-7a061c4ca1024a19e2c295d773d9642625d1c2be
  • hotfix/39.238.3
  • refactor/gitlab-auto-approve
  • feat/template-strings
  • gh-readonly-queue/next/pr-35654-137d934242c784e0c45d4b957362214f0eade1d7
  • fix/32307-global-extends-merging
  • fix/32307-global-extends-repositories
  • gh-readonly-queue/next/pr-35009-046ebf7cb84ab859f7fefceb5fa53a54ce9736f8
  • 41.55.3
  • 41.55.2
  • 41.55.1
  • 41.55.0
  • 41.54.0
  • 41.53.1
  • 41.53.0
  • 41.52.3
  • 41.52.2
  • 41.52.1
  • 41.52.0
  • 41.51.2
  • 41.51.1
  • 41.51.0
  • 41.50.0
  • 41.49.1
  • 41.49.0
  • 41.48.1
  • 41.48.0
  • 41.47.1
41 results

common.spec.ts

Blame
  • user avatar
    Johannes Feichtner authored and GitHub committed
    4039ace0
    History
    common.spec.ts 4.04 KiB
    import { logger } from '../../test/util';
    import { detectPlatform, parseJson } from './common';
    import * as hostRules from './host-rules';
    
    const validJsonString = `
    {
      "name": "John Doe",
      "age": 30,
      "city": "New York"
    }
    `;
    const invalidJsonString = `
    {
      "name": "Alice",
      "age": 25,
      "city": "Los Angeles",
      "hobbies": ["Reading", "Running", "Cooking"]
      "isStudent": true
    }
    `;
    const onlyJson5parsableString = `
    {
      name: "Bob",
      age: 35,
      city: 'San Francisco',
      // This is a comment
      "isMarried": false,
    }
    `;
    
    describe('util/common', () => {
      beforeEach(() => hostRules.clear());
    
      describe('detectPlatform', () => {
        it.each`
          url                                                                    | hostType
          ${'some-invalid@url:::'}                                               | ${null}
          ${'https://enterprise.example.com/chalk/chalk'}                        | ${null}
          ${'https://dev.azure.com/my-organization/my-project/_git/my-repo.git'} | ${'azure'}
          ${'https://myorg.visualstudio.com/my-project/_git/my-repo.git'}        | ${'azure'}
          ${'https://bitbucket.org/some-org/some-repo'}                          | ${'bitbucket'}
          ${'https://bitbucket.com/some-org/some-repo'}                          | ${'bitbucket'}
          ${'https://bitbucket.example.com/some-org/some-repo'}                  | ${'bitbucket-server'}
          ${'https://gitea.com/semantic-release/gitlab'}                         | ${'gitea'}
          ${'https://forgejo.example.com/semantic-release/gitlab'}               | ${'gitea'}
          ${'https://github.com/semantic-release/gitlab'}                        | ${'github'}
          ${'https://github-enterprise.example.com/chalk/chalk'}                 | ${'github'}
          ${'https://gitlab.com/chalk/chalk'}                                    | ${'gitlab'}
          ${'https://gitlab-enterprise.example.com/chalk/chalk'}                 | ${'gitlab'}
        `('("$url") === $hostType', ({ url, hostType }) => {
          expect(detectPlatform(url)).toBe(hostType);
        });
    
        it('uses host rules', () => {
          hostRules.add({
            hostType: 'bitbucket',
            matchHost: 'bb.example.com',
          });
          hostRules.add({
            hostType: 'gitea',
            matchHost: 'gt.example.com',
          });
          hostRules.add({
            hostType: 'github-changelog',
            matchHost: 'gh.example.com',
          });
          hostRules.add({
            hostType: 'gitlab-changelog',
            matchHost: 'gl.example.com',
          });
          hostRules.add({
            hostType: 'unknown',
            matchHost: 'f.example.com',
          });
    
          expect(detectPlatform('https://bb.example.com/chalk/chalk')).toBe(
            'bitbucket',
          );
          expect(detectPlatform('https://gt.example.com/chalk/chalk')).toBe(
            'gitea',
          );
          expect(detectPlatform('https://gh.example.com/chalk/chalk')).toBe(
            'github',
          );
          expect(detectPlatform('https://gl.example.com/chalk/chalk')).toBe(
            'gitlab',
          );
          expect(detectPlatform('https://f.example.com/chalk/chalk')).toBeNull();
        });
      });
    
      describe('parseJson', () => {
        it('returns null', () => {
          expect(parseJson(null, 'renovate.json')).toBeNull();
        });
    
        it('returns parsed json', () => {
          expect(parseJson(validJsonString, 'renovate.json')).toEqual({
            name: 'John Doe',
            age: 30,
            city: 'New York',
          });
        });
    
        it('throws error for invalid json', () => {
          expect(() => parseJson(invalidJsonString, 'renovate.json')).toThrow();
        });
    
        it('catches and warns if content parsing faield with JSON.parse but not with JSON5.parse', () => {
          expect(parseJson(onlyJson5parsableString, 'renovate.json')).toEqual({
            name: 'Bob',
            age: 35,
            city: 'San Francisco',
            isMarried: false,
          });
          expect(logger.logger.warn).toHaveBeenCalledWith(
            { context: 'renovate.json' },
            'File contents are invalid JSON but parse using JSON5. Support for this will be removed in a future release so please change to a support .json5 file name or ensure correct JSON syntax.',
          );
        });
      });
    });