summaryrefslogtreecommitdiffstats
path: root/tests/sort_simple_yaml_test.py
blob: 6cbda8577f03aaff4e1f5aae6dea9df3420fa28e (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from __future__ import annotations

import os

import pytest

from pre_commit_hooks.sort_simple_yaml import first_key
from pre_commit_hooks.sort_simple_yaml import main
from pre_commit_hooks.sort_simple_yaml import parse_block
from pre_commit_hooks.sort_simple_yaml import parse_blocks
from pre_commit_hooks.sort_simple_yaml import sort

RETVAL_GOOD = 0
RETVAL_BAD = 1
TEST_SORTS = [
    (
        ['c: true', '', 'b: 42', 'a: 19'],
        ['b: 42', 'a: 19', '', 'c: true'],
        RETVAL_BAD,
    ),

    (
        ['# i am', '# a header', '', 'c: true', '', 'b: 42', 'a: 19'],
        ['# i am', '# a header', '', 'b: 42', 'a: 19', '', 'c: true'],
        RETVAL_BAD,
    ),

    (
        ['# i am', '# a header', '', 'already: sorted', '', 'yup: i am'],
        ['# i am', '# a header', '', 'already: sorted', '', 'yup: i am'],
        RETVAL_GOOD,
    ),

    (
        ['# i am', '# a header'],
        ['# i am', '# a header'],
        RETVAL_GOOD,
    ),
]


@pytest.mark.parametrize('bad_lines,good_lines,retval', TEST_SORTS)
def test_integration_good_bad_lines(tmpdir, bad_lines, good_lines, retval):
    file_path = os.path.join(str(tmpdir), 'foo.yaml')

    with open(file_path, 'w') as f:
        f.write('\n'.join(bad_lines) + '\n')

    assert main([file_path]) == retval

    with open(file_path) as f:
        assert [line.rstrip() for line in f.readlines()] == good_lines


def test_parse_header():
    lines = ['# some header', '# is here', '', 'this is not a header']
    assert parse_block(lines, header=True) == ['# some header', '# is here']
    assert lines == ['', 'this is not a header']

    lines = ['this is not a header']
    assert parse_block(lines, header=True) == []
    assert lines == ['this is not a header']


def test_parse_block():
    # a normal block
    lines = ['a: 42', 'b: 17', '', 'c: 19']
    assert parse_block(lines) == ['a: 42', 'b: 17']
    assert lines == ['', 'c: 19']

    # a block at the end
    lines = ['c: 19']
    assert parse_block(lines) == ['c: 19']
    assert lines == []

    # no block
    lines = []
    assert parse_block(lines) == []
    assert lines == []


def test_parse_blocks():
    # normal blocks
    lines = ['a: 42', 'b: 17', '', 'c: 19']
    assert parse_blocks(lines) == [['a: 42', 'b: 17'], ['c: 19']]
    assert lines == []

    # a single block
    lines = ['a: 42', 'b: 17']
    assert parse_blocks(lines) == [['a: 42', 'b: 17']]
    assert lines == []

    # no blocks
    lines = []
    assert parse_blocks(lines) == []
    assert lines == []


def test_first_key():
    # first line
    lines = ['a: 42', 'b: 17', '', 'c: 19']
    assert first_key(lines) == 'a: 42'

    # second line
    lines = ['# some comment', 'a: 42', 'b: 17', '', 'c: 19']
    assert first_key(lines) == 'a: 42'

    # second line with quotes
    lines = ['# some comment', '"a": 42', 'b: 17', '', 'c: 19']
    assert first_key(lines) == 'a": 42'

    # no lines (not a real situation)
    lines = []
    assert first_key(lines) == ''


@pytest.mark.parametrize('bad_lines,good_lines,_', TEST_SORTS)
def test_sort(bad_lines, good_lines, _):
    assert sort(bad_lines) == good_lines