Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 95 additions & 39 deletions lib/modules/manager/cake/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,36 @@
import { codeBlock } from 'common-tags';
import upath from 'upath';
import { Fixtures } from '~test/fixtures.ts';
import { GlobalConfig } from '../../../config/global.ts';
import type { RepoGlobalConfig } from '../../../config/types.ts';
import * as nugetExtractUtil from '../nuget/util.ts';
import type { ExtractConfig } from '../types.ts';
import { extractPackageFile } from './index.ts';

const config: ExtractConfig = {};
const adminConfig: RepoGlobalConfig = {
localDir: upath.resolve('lib/modules/manager/cake/__fixtures__'),
};

describe('modules/manager/cake/index', () => {
it('extracts', () => {
expect(extractPackageFile(Fixtures.get('build.cake'))).toMatchObject({
beforeEach(() => {
// Initialize GlobalConfig with required values
GlobalConfig.set(adminConfig);
});

afterEach(() => {
// Clean up
GlobalConfig.reset();
Comment thread
papauorg marked this conversation as resolved.
Resolved
});

it('extracts', async () => {
expect(
await extractPackageFile(
Fixtures.get('build.cake'),
'build.cake',
config,
),
).toMatchObject({
deps: [
{ depName: 'Foo.Foo', currentValue: undefined },
{ depName: 'Bim.Bim', currentValue: '6.6.6' },
Expand All @@ -21,7 +47,7 @@ describe('modules/manager/cake/index', () => {
});
});

it('extracts dotnet tools from single sdk style build file', () => {
it('extracts dotnet tools from single sdk style build file', async () => {
const content = codeBlock`
#:sdk Cake.Sdk

Expand All @@ -47,35 +73,37 @@ describe('modules/manager/cake/index', () => {

RunTarget(target);
`;
expect(extractPackageFile(content)).toMatchObject({
deps: [
{
depName: 'SingleTool.Install.First',
currentValue: '1.0.0',
datasource: 'nuget',
registryUrls: ['https://api.nuget.org/v3/index.json'],
},
{
depName: 'SingleTool.Install.Second',
currentValue: '1.2.0',
datasource: 'nuget',
},
{
depName: 'MultipleTools.Install.First',
currentValue: '2.0.0',
datasource: 'nuget',
registryUrls: ['https://api.nuget.org/v3/index.json'],
},
{
depName: 'MultipleTools.Install.Second',
currentValue: '2.1.1',
datasource: 'nuget',
},
],
});
expect(await extractPackageFile(content, 'build.cs', config)).toMatchObject(
{
deps: [
{
depName: 'SingleTool.Install.First',
currentValue: '1.0.0',
datasource: 'nuget',
registryUrls: ['https://api.nuget.org/v3/index.json'],
},
{
depName: 'SingleTool.Install.Second',
currentValue: '1.2.0',
datasource: 'nuget',
},
{
depName: 'MultipleTools.Install.First',
currentValue: '2.0.0',
datasource: 'nuget',
registryUrls: ['https://api.nuget.org/v3/index.json'],
},
{
depName: 'MultipleTools.Install.Second',
currentValue: '2.1.1',
datasource: 'nuget',
},
],
},
);
});

it('skips invalid entries in InstallTools', () => {
it('skips invalid entries in InstallTools', async () => {
const content = codeBlock`
#:sdk Cake.Sdk

Expand All @@ -85,14 +113,42 @@ describe('modules/manager/cake/index', () => {
"dotnet:?package=Good.Tool&version=1.2.3"
);
`;
expect(extractPackageFile(content)).toMatchObject({
deps: [
{
depName: 'Good.Tool',
currentValue: '1.2.3',
datasource: 'nuget',
},
],
});
expect(await extractPackageFile(content, 'build.cs', config)).toMatchObject(
{
deps: [
{
depName: 'Good.Tool',
currentValue: '1.2.3',
datasource: 'nuget',
},
],
},
);
});

it('calls applyRegistries to honor nuget.config files if present for .cake files', async () => {
const applyRegistriesSpy = vi
.spyOn(nugetExtractUtil, 'applyRegistries')
.mockImplementation((deps: any) => deps);
Comment thread
papauorg marked this conversation as resolved.
Resolved

const content = codeBlock`#addin nuget:?package=Contoso.SomePackage&version=1.2.3`;
await extractPackageFile(content, 'build.cake', config);

expect(applyRegistriesSpy).toHaveBeenCalled();
Comment thread
papauorg marked this conversation as resolved.
Resolved
});

it('calls applyRegistries to honor nuget.config files if present for InstallTools', async () => {
const applyRegistriesSpy = vi
.spyOn(nugetExtractUtil, 'applyRegistries')
.mockImplementation((deps: any) => deps);
Comment thread
papauorg marked this conversation as resolved.
Resolved

const content = codeBlock`
#:sdk Cake.Sdk

InstallTools("dotnet:?package=Good.Tool&version=1.2.3");
`;
await extractPackageFile(content, 'build.cs', config);

expect(applyRegistriesSpy).toHaveBeenCalled();
});
});
41 changes: 30 additions & 11 deletions lib/modules/manager/cake/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import type { Category } from '../../../constants/index.ts';
import { regEx } from '../../../util/regex.ts';
import { isHttpUrl } from '../../../util/url.ts';
import { NugetDatasource } from '../../datasource/nuget/index.ts';
import type { PackageDependency, PackageFileContent } from '../types.ts';
import type { Registry } from '../nuget/types.ts';
import { applyRegistries, getConfiguredRegistries } from '../nuget/util.ts';
import type {
ExtractConfig,
PackageDependency,
PackageFileContent,
} from '../types.ts';

export const url = 'https://cakebuild.net/docs';
export const categories: Category[] = ['dotnet'];
Expand Down Expand Up @@ -67,25 +73,38 @@ function parseDependencyLine(line: string): PackageDependency | null {
}
}

export function extractPackageFile(content: string): PackageFileContent {
const deps: PackageDependency[] = [];
function parseAndPushDependencyLine(
registries: Registry[] | undefined,
deps: PackageDependency[],
value: string,
): PackageDependency[] {
Comment thread
papauorg marked this conversation as resolved.
Resolved
const dep = parseDependencyLine(value);
if (dep) {
applyRegistries(dep as any, registries);
Comment thread
papauorg marked this conversation as resolved.
Resolved
deps.push(dep);
}
return deps;
Comment thread
papauorg marked this conversation as resolved.
Resolved
}

export async function extractPackageFile(
content: string,
packageFile: string,
_config: ExtractConfig,
): Promise<PackageFileContent | null> {
Comment thread
papauorg marked this conversation as resolved.
Resolved
let deps: PackageDependency[] = [];
const registries = await getConfiguredRegistries(packageFile);

lexer.reset(content);
let token = lexer.next();
while (token) {
const { type, value } = token;
if (type === 'dependency' || type === 'dependencyQuoted') {
const dep = parseDependencyLine(value);
if (dep) {
deps.push(dep);
}
deps = parseAndPushDependencyLine(registries, deps, value);
Comment thread
papauorg marked this conversation as resolved.
Resolved
} else if (type === 'dependencyFromInstallTools') {
const matches = value.matchAll(regEx(/"dotnet:[^"]+"/g));
for (const match of matches) {
const withoutQuote = match.toString().slice(1, -1);
const dep = parseDependencyLine(withoutQuote);
if (dep) {
deps.push(dep);
}
deps = parseAndPushDependencyLine(registries, deps, withoutQuote);
Comment thread
papauorg marked this conversation as resolved.
Resolved
}
}
token = lexer.next();
Expand Down