Skip to content
Snippets Groups Projects
Select Git revision
  • 06d8c270433206eecba4f60635f669cd8c5393c1
  • 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.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.47.0
  • 41.46.8
  • 41.46.7
  • 41.46.6
  • 41.46.5
  • 41.46.4
  • 41.46.3
  • 41.46.2
41 results

extract.spec.ts

Blame
  • user avatar
    Maksim authored and GitHub committed
    Co-authored-by: default avatarMichael Kriese <michael.kriese@visualon.de>
    06d8c270
    History
    extract.spec.ts 9.45 KiB
    import * as upath from 'upath';
    import { loadFixture } from '../../../test/util';
    import { GlobalConfig } from '../../config/global';
    import type { RepoGlobalConfig } from '../../config/types';
    import type { ExtractConfig } from '../types';
    import { extractPackageFile } from './extract';
    
    const config: ExtractConfig = {};
    
    const adminConfig: RepoGlobalConfig = {
      localDir: upath.resolve('lib/manager/nuget/__fixtures__'),
    };
    
    describe('manager/nuget/extract', () => {
      describe('extractPackageFile()', () => {
        beforeEach(() => {
          GlobalConfig.set(adminConfig);
        });
        afterEach(() => {
          GlobalConfig.reset();
        });
        it('returns empty for invalid csproj', async () => {
          expect(await extractPackageFile('nothing here', 'bogus', config)).toEqual(
            { deps: [] }
          );
        });
        it('extracts package version dependency', async () => {
          const packageFile =
            'with-centralized-package-versions/Directory.Packages.props';
          const sample = loadFixture(packageFile);
          const res = await extractPackageFile(sample, packageFile, config);
          expect(res.deps).toMatchSnapshot();
          expect(res.deps).toHaveLength(1);
        });
        it('extracts all dependencies', async () => {
          const packageFile = 'sample.csproj';
          const sample = loadFixture(packageFile);
          const res = await extractPackageFile(sample, packageFile, config);
          expect(res.deps).toMatchSnapshot();
          expect(res.deps).toHaveLength(17);
        });
        it('extracts all dependencies from global packages file', async () => {
          const packageFile = 'packages.props';
          const sample = loadFixture(packageFile);
          const res = await extractPackageFile(sample, packageFile, config);
          expect(res.deps).toMatchSnapshot();
          expect(res.deps).toHaveLength(17);
        });
        it('considers NuGet.config', async () => {
          const packageFile = 'with-config-file/with-config-file.csproj';
          const contents = loadFixture(packageFile);
          expect(await extractPackageFile(contents, packageFile, config)).toEqual({
            deps: [
              {
                currentValue: '4.5.0',
                datasource: 'nuget',
                depName: 'Autofac',
                depType: 'nuget',
                registryUrls: [
                  'https://api.nuget.org/v3/index.json#protocolVersion=3',
                  'https://contoso.com/packages/',
                ],
              },
            ],
          });
        });
        it('considers lower-case nuget.config', async () => {
          const packageFile =
            'with-lower-case-config-file/with-lower-case-config-file.csproj';
          const contents = loadFixture(packageFile);
          expect(await extractPackageFile(contents, packageFile, config)).toEqual({
            deps: [
              {
                currentValue: '4.5.0',
                datasource: 'nuget',
                depName: 'Autofac',
                depType: 'nuget',
                registryUrls: [
                  'https://api.nuget.org/v3/index.json#protocolVersion=3',
                  'https://contoso.com/packages/',
                ],
              },
            ],
          });
        });
        it('considers pascal-case NuGet.Config', async () => {
          const packageFile =
            'with-pascal-case-config-file/with-pascal-case-config-file.csproj';
          const contents = loadFixture(packageFile);
          expect(await extractPackageFile(contents, packageFile, config)).toEqual({
            deps: [
              {
                currentValue: '4.5.0',
                datasource: 'nuget',
                depName: 'Autofac',
                depType: 'nuget',
                registryUrls: [
                  'https://api.nuget.org/v3/index.json#protocolVersion=3',
                  'https://contoso.com/packages/',
                ],
              },
            ],
          });
        });
        it('handles malformed NuGet.config', async () => {
          const packageFile =
            'with-malformed-config-file/with-malformed-config-file.csproj';
          const contents = loadFixture(packageFile);
          expect(await extractPackageFile(contents, packageFile, config)).toEqual({
            deps: [
              {
                currentValue: '4.5.0',
                datasource: 'nuget',
                depName: 'Autofac',
                depType: 'nuget',
              },
            ],
          });
        });
        it('handles NuGet.config without package sources', async () => {
          const packageFile =
            'without-package-sources/without-package-sources.csproj';
          const contents = loadFixture(packageFile);
          expect(await extractPackageFile(contents, packageFile, config)).toEqual({
            deps: [
              {
                currentValue: '4.5.0',
                datasource: 'nuget',
                depName: 'Autofac',
                depType: 'nuget',
              },
            ],
          });
        });
        it('ignores local feed in NuGet.config', async () => {
          const packageFile =
            'with-local-feed-in-config-file/with-local-feed-in-config-file.csproj';
          const contents = loadFixture(packageFile);
          expect(await extractPackageFile(contents, packageFile, config)).toEqual({
            deps: [
              {
                currentValue: '4.5.0',
                datasource: 'nuget',
                depName: 'Autofac',
                depType: 'nuget',
                registryUrls: ['https://contoso.com/packages/'],
              },
            ],
          });
        });
        it('extracts registry URLs independently', async () => {
          const packageFile = 'multiple-package-files/one/one.csproj';
          const contents = loadFixture(packageFile);
          const otherPackageFile = 'multiple-package-files/two/two.csproj';
          const otherContents = loadFixture(otherPackageFile);
          expect(await extractPackageFile(contents, packageFile, config)).toEqual({
            deps: [
              {
                currentValue: '4.5.0',
                datasource: 'nuget',
                depName: 'Autofac',
                depType: 'nuget',
                registryUrls: [
                  'https://api.nuget.org/v3/index.json',
                  'https://example.org/one',
                ],
              },
            ],
          });
          expect(
            await extractPackageFile(otherContents, otherPackageFile, config)
          ).toEqual({
            deps: [
              {
                currentValue: '4.5.0',
                datasource: 'nuget',
                depName: 'Autofac',
                depType: 'nuget',
                registryUrls: [
                  'https://api.nuget.org/v3/index.json',
                  'https://example.org/two',
                ],
              },
            ],
          });
        });
    
        it('extracts msbuild-sdks from global.json', async () => {
          const packageFile = 'msbuild-sdk-files/global.json';
          const contents = loadFixture(packageFile);
          expect(
            await extractPackageFile(contents, packageFile, config)
          ).toMatchObject({
            deps: [
              {
                currentValue: '5.0.302',
                depName: 'dotnet-sdk',
                depType: 'dotnet-sdk',
                skipReason: 'unsupported-datasource',
              },
              {
                currentValue: '0.2.0',
                datasource: 'nuget',
                depName: 'YoloDev.Sdk',
                depType: 'msbuild-sdk',
              },
            ],
          });
        });
    
        it('extracts dotnet-sdk from global.json', async () => {
          const packageFile = 'msbuild-sdk-files/global.1.json';
          const contents = loadFixture(packageFile);
          expect(
            await extractPackageFile(contents, 'global.json', config)
          ).toMatchObject({
            deps: [
              {
                currentValue: '5.0.302',
                depName: 'dotnet-sdk',
                depType: 'dotnet-sdk',
                skipReason: 'unsupported-datasource',
              },
            ],
          });
        });
    
        it('handles malformed global.json', async () => {
          const packageFile = 'msbuild-sdk-files/invalid-json/global.json';
          const contents = loadFixture(packageFile);
          expect(
            await extractPackageFile(contents, packageFile, config)
          ).toBeNull();
        });
    
        it('handles not-a-nuget global.json', async () => {
          const packageFile = 'msbuild-sdk-files/not-nuget/global.json';
          const contents = loadFixture(packageFile);
          expect(
            await extractPackageFile(contents, packageFile, config)
          ).toBeNull();
        });
    
        describe('.config/dotnet-tools.json', () => {
          const packageFile = '.config/dotnet-tools.json';
          const contents = loadFixture('dotnet-tools.json');
    
          it('works', async () => {
            expect(await extractPackageFile(contents, packageFile, config)).toEqual(
              {
                deps: [
                  {
                    currentValue: '2.0.0',
                    datasource: 'nuget',
                    depName: 'minver-cli',
                    depType: 'nuget',
                  },
                ],
              }
            );
          });
    
          it('with-config', async () => {
            expect(
              await extractPackageFile(
                contents,
                `with-config-file/${packageFile}`,
                config
              )
            ).toEqual({
              deps: [
                {
                  currentValue: '2.0.0',
                  datasource: 'nuget',
                  depName: 'minver-cli',
                  depType: 'nuget',
                  registryUrls: [
                    'https://api.nuget.org/v3/index.json#protocolVersion=3',
                    'https://contoso.com/packages/',
                  ],
                },
              ],
            });
          });
    
          it('wrong version', async () => {
            expect(
              await extractPackageFile(
                contents.replace('"version": 1,', '"version": 2,'),
                packageFile,
                config
              )
            ).toBeNull();
          });
    
          it('does not throw', async () => {
            expect(await extractPackageFile('{{', packageFile, config)).toBeNull();
          });
        });
      });
    });