Skip to content

Commit

Permalink
test(yaml): add test for YAML tags
Browse files Browse the repository at this point in the history
  • Loading branch information
websi committed Sep 18, 2024
1 parent dce6912 commit 33861e4
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
services:
web:
image: node:20.0.0
build:
context: '.'
dockerfile: 'Dockerfile'
ports:
- "80:8000"

worker:
extends:
service: web
build: !reset null
ports: !override
- "443:8000"

db:
image: "postgres:9.4.0"
volumes:
- db-data:/var/lib/postgresql/data
networks:
- backend
deploy:
placement:
constraints: [node.role == manager]
18 changes: 18 additions & 0 deletions lib/modules/manager/docker-compose/extract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const yamlFile1 = Fixtures.get('docker-compose.1.yml');
const yamlFile3 = Fixtures.get('docker-compose.3.yml');
const yamlFile3NoVersion = Fixtures.get('docker-compose.3-no-version.yml');
const yamlFile3DefaultValue = Fixtures.get('docker-compose.3-default-val.yml');
const yamlFile3WithTags = Fixtures.get('docker-compose.3-tags.yml');

describe('modules/manager/docker-compose/extract', () => {
describe('extractPackageFile()', () => {
Expand Down Expand Up @@ -56,6 +57,23 @@ describe('modules/manager/docker-compose/extract', () => {
expect(res?.deps).toHaveLength(1);
});

it('extracts can parse yaml tags for version 3', () => {
const res = extractPackageFile(yamlFile3WithTags, '', {});
expect(res?.deps).toMatchInlineSnapshot(`
[
{
"autoReplaceStringTemplate": "{{depName}}{{#if newValue}}:{{newValue}}{{/if}}{{#if newDigest}}@{{newDigest}}{{/if}}",
"currentDigest": undefined,
"currentValue": "9.4.0",
"datasource": "docker",
"depName": "postgres",
"replaceString": "postgres:9.4.0",
},
]
`);
expect(res?.deps).toHaveLength(1);
});

it('extracts image and replaces registry', () => {
const compose = codeBlock`
version: "3"
Expand Down
3 changes: 1 addition & 2 deletions lib/modules/manager/gitlabci-include/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import type {
GitlabIncludeProject,
GitlabPipeline,
} from '../gitlabci/types';
import { replaceReferenceTags } from '../gitlabci/utils';
import type { PackageDependency, PackageFileContent } from '../types';

function extractDepFromIncludeFile(
Expand Down Expand Up @@ -72,7 +71,7 @@ export function extractPackageFile(
const endpoint = GlobalConfig.get('endpoint');
try {
// TODO: use schema (#9610)
const doc = parseSingleYaml<GitlabPipeline>(replaceReferenceTags(content));
const doc = parseSingleYaml<GitlabPipeline>(content);
const includes = getAllIncludeProjects(doc);
for (const includeObj of includes) {
const dep = extractDepFromIncludeFile(includeObj);
Expand Down
10 changes: 6 additions & 4 deletions lib/modules/manager/gitlabci/common.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { codeBlock } from 'common-tags';
import { parseSingleYaml } from '../../../util/yaml';
import type { GitlabPipeline } from '../gitlabci/types';
import { replaceReferenceTags } from '../gitlabci/utils';
import {
filterIncludeFromGitlabPipeline,
isGitlabIncludeComponent,
Expand All @@ -12,7 +11,7 @@ import {

// TODO: use schema (#9610)
const pipeline = parseSingleYaml<GitlabPipeline>(
replaceReferenceTags(codeBlock`
codeBlock`
include:
- project: mikebryant/include-source-example
file: /template.yaml
Expand All @@ -25,7 +24,7 @@ const pipeline = parseSingleYaml<GitlabPipeline>(
script:
- !reference [.setup, script]
- !reference [arbitrary job name with space and no starting dot, nested1, nested2, nested3]`),
- !reference [arbitrary job name with space and no starting dot, nested1, nested2, nested3]`,
);
const includeLocal = { local: 'something' };
const includeProject = { project: 'something' };
Expand All @@ -37,7 +36,10 @@ describe('modules/manager/gitlabci/common', () => {
const filtered_pipeline = filterIncludeFromGitlabPipeline(pipeline);
expect(filtered_pipeline).not.toHaveProperty('include');
expect(filtered_pipeline).toEqual({
script: [null, null],
script: [
['.setup', 'script'],
['arbitrary job name with space and no starting dot', 'nested1', 'nested2', 'nested3']
],
});
});
});
Expand Down
11 changes: 1 addition & 10 deletions lib/modules/manager/gitlabci/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Fixtures } from '../../../../test/fixtures';
import type { PackageDependency } from '../types';
import { getGitlabDep, replaceReferenceTags } from './utils';
import { getGitlabDep } from './utils';

describe('modules/manager/gitlabci/utils', () => {
describe('getGitlabDep', () => {
Expand Down Expand Up @@ -82,12 +81,4 @@ describe('modules/manager/gitlabci/utils', () => {
});
});
});

describe('replaceReferenceTags', () => {
it('replaces all !reference tags with empty strings', () => {
const yamlFileReferenceConfig = Fixtures.get('gitlab-ci.reference.yaml');
const replaced = replaceReferenceTags(yamlFileReferenceConfig);
expect(replaced).not.toContain('!reference');
});
});
});
13 changes: 0 additions & 13 deletions lib/modules/manager/gitlabci/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,6 @@ import { regEx } from '../../../util/regex';
import { getDep } from '../dockerfile/extract';
import type { PackageDependency } from '../types';

const re = /!reference \[[^\]]+\]/g;

/**
* Replaces GitLab reference tags before parsing, because our yaml parser cannot process them anyway.
* @param content pipeline yaml
* @returns replaced pipeline content
* https://docs.gitlab.com/ee/ci/yaml/#reference-tags
*/
export function replaceReferenceTags(content: string): string {
const res = content.replace(re, '');
return res;
}

const depProxyRe = regEx(
`(?<prefix>\\$\\{?CI_DEPENDENCY_PROXY_(?:DIRECT_)?GROUP_IMAGE_PREFIX\\}?/)(?<depName>.+)`,
);
Expand Down
18 changes: 18 additions & 0 deletions lib/util/yaml.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,5 +281,23 @@ describe('util/yaml', () => {
},
});
});

it('should parse content with yaml tags', () => {
expect(
parseSingleYaml(
codeBlock`
myObject:
aString: value
aStringWithTag: !reset null
`,
{ removeTemplates: true },
),
).toEqual({
myObject: {
aString: 'value',
aStringWithTag: "null",
},
});
});
});
});

0 comments on commit 33861e4

Please sign in to comment.