Welcome to mirror list, hosted at ThFree Co, Russian Federation.

yaml_spec.js « utils « lib « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d1ce00130e22bd6c26f00d667e54ecaa18468374 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { Document, parseDocument } from 'yaml';
import { merge } from '~/lib/utils/yaml';

// Mock data for Comments on pairs
const COMMENTS_ON_PAIRS_SOURCE = `foo:
  # barbaz
  bar: baz

  # bazboo
  baz: boo
`;

const COMMENTS_ON_PAIRS_TARGET = `foo:
  # abcdef
  abc: def
  # boobaz
  boo: baz
`;

const COMMENTS_ON_PAIRS_EXPECTED = `foo:
  # abcdef
  abc: def
  # boobaz
  boo: baz
  # barbaz
  bar: baz

  # bazboo
  baz: boo
`;

// Mock data for Comments on seqs
const COMMENTS_ON_SEQS_SOURCE = `foo:
  # barbaz
  - barbaz
  # bazboo
  - baz: boo
`;

const COMMENTS_ON_SEQS_TARGET = `foo:
  # abcdef
  - abcdef

  # boobaz
  - boobaz
`;

const COMMENTS_ON_SEQS_EXPECTED = `foo:
  # abcdef
  - abcdef

  # boobaz
  - boobaz
  # barbaz
  - barbaz
  # bazboo
  - baz: boo
`;

describe('Yaml utility functions', () => {
  describe('merge', () => {
    const getAsNode = (yamlStr) => {
      return parseDocument(yamlStr).contents;
    };

    describe('Merge two Nodes', () => {
      it.each`
        scenario                               | source                                       | target                                         | options                      | expected
        ${'merge a map'}                       | ${getAsNode('foo:\n  bar: baz\n')}           | ${'foo:\n  abc: def\n'}                        | ${undefined}                 | ${'foo:\n  abc: def\n  bar: baz\n'}
        ${'merge a seq'}                       | ${getAsNode('foo:\n  - bar\n')}              | ${'foo:\n  - abc\n'}                           | ${undefined}                 | ${'foo:\n  - bar\n'}
        ${'merge-append seqs'}                 | ${getAsNode('foo:\n  - bar\n')}              | ${'foo:\n  - abc\n'}                           | ${{ onSequence: 'append' }}  | ${'foo:\n  - abc\n  - bar\n'}
        ${'merge-replace a seq'}               | ${getAsNode('foo:\n  - bar\n')}              | ${'foo:\n  - abc\n'}                           | ${{ onSequence: 'replace' }} | ${'foo:\n  - bar\n'}
        ${'override existing paths'}           | ${getAsNode('foo:\n  bar: baz\n')}           | ${'foo:\n  bar: boo\n'}                        | ${undefined}                 | ${'foo:\n  bar: baz\n'}
        ${'deep maps'}                         | ${getAsNode('foo:\n  bar:\n    abc: def\n')} | ${'foo:\n  bar:\n    baz: boo\n  jkl:  mno\n'} | ${undefined}                 | ${'foo:\n  bar:\n    baz: boo\n    abc: def\n  jkl: mno\n'}
        ${'append maps inside seqs'}           | ${getAsNode('foo:\n  - abc: def\n')}         | ${'foo:\n  - bar: baz\n'}                      | ${{ onSequence: 'append' }}  | ${'foo:\n  - bar: baz\n  - abc: def\n'}
        ${'inexistent paths create new nodes'} | ${getAsNode('foo:\n  bar: baz\n')}           | ${'abc: def\n'}                                | ${undefined}                 | ${'abc: def\nfoo:\n  bar: baz\n'}
        ${'document as source'}                | ${parseDocument('foo:\n  bar: baz\n')}       | ${'foo:\n  abc: def\n'}                        | ${undefined}                 | ${'foo:\n  abc: def\n  bar: baz\n'}
        ${'object as source'}                  | ${{ foo: { bar: 'baz' } }}                   | ${'foo:\n  abc: def\n'}                        | ${undefined}                 | ${'foo:\n  abc: def\n  bar: baz\n'}
        ${'comments on pairs'}                 | ${parseDocument(COMMENTS_ON_PAIRS_SOURCE)}   | ${COMMENTS_ON_PAIRS_TARGET}                    | ${undefined}                 | ${COMMENTS_ON_PAIRS_EXPECTED}
        ${'comments on seqs'}                  | ${parseDocument(COMMENTS_ON_SEQS_SOURCE)}    | ${COMMENTS_ON_SEQS_TARGET}                     | ${{ onSequence: 'append' }}  | ${COMMENTS_ON_SEQS_EXPECTED}
      `('$scenario', ({ source, target, expected, options }) => {
        const targetDoc = parseDocument(target);
        merge(targetDoc, source, options);
        const expectedDoc = parseDocument(expected);
        expect(targetDoc.toString()).toEqual(expectedDoc.toString());
      });

      it('type conflict will throw an Error', () => {
        const sourceDoc = parseDocument('foo:\n  bar:\n    - baz\n');
        const targetDoc = parseDocument('foo:\n  bar: def\n');
        expect(() => merge(targetDoc, sourceDoc)).toThrow(
          'Type conflict at "foo.bar": Destination node is of type Scalar, the node' +
            ' to be merged is of type YAMLSeq',
        );
      });

      it('merging a collection into an empty doc', () => {
        const targetDoc = new Document();
        merge(targetDoc, { foo: { bar: 'baz' } });
        const expected = parseDocument('foo:\n  bar: baz\n');
        expect(targetDoc.toString()).toEqual(expected.toString());
      });
    });
  });
});