Skip to content
Snippets Groups Projects
Select Git revision
  • b0cc608045c1142bd68bff7fea8c3663d56d89df
  • main default protected
  • renovate/main-ghcr.io-renovatebot-base-image-11.x
  • refactor/pin-new-value
  • fix/user-agent
  • feat/37517-base64-private-key
  • next
  • feat/gnupg
  • fix/36615b-branch-reuse-no-cache
  • chore/punycode
  • 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
  • 41.97.7
  • 41.97.6
  • 41.97.5
  • 41.97.4
  • 41.97.3
  • 41.97.2
  • 41.97.1
  • 41.97.0
  • 41.96.2
  • 41.96.1
  • 41.96.0
  • 41.95.2
  • 41.95.1
  • 41.95.0
  • 41.94.0
  • 41.93.3
  • 41.93.2
  • 41.93.1
  • 41.93.0
  • 41.92.1
41 results

index.spec.ts

Blame
  • user avatar
    Sebastian Kral authored and GitHub committed
    ea0b324e
    History
    index.spec.ts 3.90 KiB
    import { getOptions } from '../config/options';
    import { loadModules } from '../util/modules';
    import { isVersioningApiConstructor } from './common';
    import { GenericVersion, GenericVersioningApi } from './loose/generic';
    import * as semverVersioning from './semver';
    import type { VersioningApi, VersioningApiConstructor } from './types';
    import * as allVersioning from '.';
    
    const supportedSchemes = getOptions().find(
      (option) => option.name === 'versioning'
    ).allowedValues;
    
    describe('versioning/index', () => {
      it('has api', () => {
        // FIXME: explicit assert condition
        expect(Object.keys(allVersioning.get('semver')).sort()).toMatchSnapshot();
      });
      it('validates', () => {
        function validate(
          module: VersioningApi | VersioningApiConstructor,
          name: string
        ): boolean {
          // eslint-disable-next-line new-cap
          const mod = isVersioningApiConstructor(module) ? new module() : module;
    
          // TODO: test required api (#9715)
          if (!mod.isValid || !mod.isVersion) {
            throw Error(`Missing api on ${name}`);
          }
    
          return true;
        }
        const vers = allVersioning.getVersionings();
    
        const loadedVers = loadModules(__dirname);
        expect(Array.from(vers.keys())).toEqual(Object.keys(loadedVers));
    
        for (const name of vers.keys()) {
          const ver = vers.get(name);
          expect(validate(ver, name)).toBe(true);
        }
      });
    
      it('should fallback to semver', () => {
        expect(allVersioning.get(undefined)).toBe(
          allVersioning.get(semverVersioning.id)
        );
        expect(allVersioning.get('unknown')).toBe(
          allVersioning.get(semverVersioning.id)
        );
      });
    
      it('should accept config', () => {
        expect(allVersioning.get('semver:test')).toBeDefined();
      });
    
      describe('should return the same interface', () => {
        const optionalFunctions = [
          'isLessThanRange',
          'valueToVersion',
          'constructor',
          'hasOwnProperty',
          'isPrototypeOf',
          'propertyIsEnumerable',
          'should',
          'toLocaleString',
          'toString',
          'valueOf',
        ];
        const npmApi = Object.keys(allVersioning.get(semverVersioning.id))
          .filter((val) => !optionalFunctions.includes(val))
          .sort();
    
        function getAllPropertyNames(obj: any): string[] {
          const props = [];
          let o = obj;
    
          do {
            Object.getOwnPropertyNames(o).forEach((prop) => {
              if (!props.includes(prop)) {
                props.push(prop);
              }
            });
            // eslint-disable-next-line no-cond-assign
          } while ((o = Object.getPrototypeOf(o)));
    
          return props;
        }
    
        for (const supportedScheme of supportedSchemes) {
          it(supportedScheme, () => {
            const schemeKeys = getAllPropertyNames(
              allVersioning.get(supportedScheme)
            )
              .filter(
                (val) => !optionalFunctions.includes(val) && !val.startsWith('_')
              )
              .sort();
    
            expect(schemeKeys).toEqual(npmApi);
    
            const apiOrCtor = require('./' + supportedScheme).api;
            if (isVersioningApiConstructor(apiOrCtor)) {
              return;
            }
    
            expect(Object.keys(apiOrCtor).sort()).toEqual(
              Object.keys(allVersioning.get(supportedScheme)).sort()
            );
          });
        }
    
        it('dummy', () => {
          class DummyScheme extends GenericVersioningApi {
            // eslint-disable-next-line class-methods-use-this
            protected override _compare(_version: string, _other: string): number {
              throw new Error('Method not implemented.');
            }
    
            // eslint-disable-next-line class-methods-use-this
            protected _parse(_version: string): GenericVersion {
              throw new Error('Method not implemented.');
            }
          }
    
          const api = new DummyScheme();
          const schemeKeys = getAllPropertyNames(api)
            .filter(
              (val) => !optionalFunctions.includes(val) && !val.startsWith('_')
            )
            .sort();
    
          expect(schemeKeys).toEqual(npmApi);
        });
      });
    });