2
2
3
3
import pytest
4
4
5
- from ops2deb .formatter import format_blueprint , format_description
5
+ from ops2deb .formatter import format_blueprint , format_description , sort_blueprints
6
6
from ops2deb .parser import Blueprint
7
7
8
8
description_with_empty_line = """
17
17
"""
18
18
19
19
20
- def test_format_description_should_only_remove_empty_lines_at_start_or_end ():
20
+ def test_format_description__should_only_remove_empty_lines_at_start_or_end ():
21
21
result = format_description (description_with_empty_line )
22
22
assert result [0 ] != "\n "
23
23
assert result [- 1 ] != "\n "
24
24
assert "\n " in result
25
25
26
26
27
- def test_format_description_should_remove_trailing_spaces ():
27
+ def test_format_description__should_remove_trailing_spaces ():
28
28
lines = description_with_empty_line .split ("\n " )
29
29
lines = [line + " " for line in lines ]
30
30
description_with_trailing_spaces = "\n " .join (lines )
@@ -33,20 +33,20 @@ def test_format_description_should_remove_trailing_spaces():
33
33
assert result [1 ] == ""
34
34
35
35
36
- def test_format_description_should_wrap_long_lines ():
36
+ def test_format_description__should_wrap_long_lines ():
37
37
result = format_description (description_with_long_line )
38
38
assert len (result .split ("\n " )) == 2
39
39
40
40
41
41
@pytest .mark .parametrize (
42
42
"description" , [description_with_empty_line , description_with_long_line ]
43
43
)
44
- def test_format_description_should_be_idempotent (description ):
44
+ def test_format_description__should_be_idempotent (description ):
45
45
result = format_description (description )
46
46
assert format_description (result ) == result
47
47
48
48
49
- def test_format_blueprint_should_remove_default_values ():
49
+ def test_format_blueprint__should_remove_default_values ():
50
50
raw_blueprint = dict (
51
51
name = "great-app" ,
52
52
version = "1.0.0" ,
@@ -59,20 +59,20 @@ def test_format_blueprint_should_remove_default_values():
59
59
assert format_blueprint (raw_blueprint_with_defaults ) == raw_blueprint
60
60
61
61
62
- def test_format_blueprint_should_not_remove_field_when_value_is_not_default (
62
+ def test_format_blueprint__should_not_remove_field_when_value_is_not_default (
63
63
blueprint_factory ,
64
64
):
65
65
blueprint = blueprint_factory (revision = 2 , depends = ["test" ])
66
66
blueprint = format_blueprint (blueprint .dict ())
67
67
assert {"revision" , "depends" , "fetch" , "script" }.issubset (blueprint .keys ())
68
68
69
69
70
- def test_format_blueprint_should_not_render_templated_values (blueprint_factory ):
70
+ def test_format_blueprint__should_not_render_templated_values (blueprint_factory ):
71
71
blueprint = blueprint_factory (version = "{{env('TEST', 0)}}" , construct = True )
72
72
assert format_blueprint (blueprint .dict ())["version" ] == "{{env('TEST', 0)}}"
73
73
74
74
75
- def test_format_blueprint_should_replace_fetch_object_with_string_when_only_key_is_url ():
75
+ def test_format_blueprint__replaces_fetch_object_with_string_when_only_key_is_url ():
76
76
raw_blueprint = dict (
77
77
name = "great-app" ,
78
78
version = "1.0.0" ,
@@ -82,13 +82,55 @@ def test_format_blueprint_should_replace_fetch_object_with_string_when_only_key_
82
82
assert format_blueprint (raw_blueprint )["fetch" ] == "http://test/app.tar.gz"
83
83
84
84
85
- def test_format_blueprint_should_replace_field_arch_by_architecture ():
86
- raw_blueprint = dict (
87
- name = "great-app" ,
88
- version = "1.0.0" ,
89
- arch = "all" ,
90
- summary = "A summary" ,
85
+ def test_sort_blueprints__sorts_by_name_and_version_when_blueprint_uses_semver ():
86
+ # Given
87
+ blueprint_0 = dict ( name = "great-app" , version = "2.0.0" , summary = "A summary" )
88
+ blueprint_1 = dict ( name = "great-app" , version = "1.0.0" , summary = "A summary" )
89
+ blueprint_2 = dict (
90
+ name = "great-app" , matrix = dict ( versions = [ "0.1.0" , "0.2.0" ]), summary = "A summary"
91
91
)
92
- formatted_blueprint = format_blueprint (raw_blueprint )
93
- assert formatted_blueprint ["architecture" ] == "all"
94
- assert "arch" not in formatted_blueprint
92
+
93
+ # When
94
+ result = sort_blueprints ([blueprint_0 , blueprint_1 , blueprint_2 ])
95
+
96
+ # Then
97
+ assert result == [blueprint_2 , blueprint_1 , blueprint_0 ]
98
+
99
+
100
+ def test_sort_blueprints__does_not_sort_by_version_when_blueprint_does_not_use_semver ():
101
+ # Given
102
+ blueprint_0 = dict (name = "great-app" , version = "2020" , summary = "A summary" )
103
+ blueprint_1 = dict (name = "great-app" , version = "2019" , summary = "A summary" )
104
+ blueprint_2 = dict (name = "great-app" , version = "2023" , summary = "A summary" )
105
+
106
+ # When
107
+ result = sort_blueprints ([blueprint_0 , blueprint_1 , blueprint_2 ])
108
+
109
+ # Then
110
+ assert result == [blueprint_0 , blueprint_1 , blueprint_2 ]
111
+
112
+
113
+ def test_sort_blueprints__sorts_by_name_version_and_revision_when_revision_is_an_int ():
114
+ # Given
115
+ blueprint_0 = dict (name = "great-app" , version = "1.0.0" , summary = "A summary" , revision = 2 )
116
+ blueprint_1 = dict (name = "great-app" , version = "1.0.0" , summary = "A summary" , revision = 3 )
117
+ blueprint_2 = dict (name = "great-app" , version = "1.0.0" , summary = "A summary" , revision = 1 )
118
+
119
+ # When
120
+ result = sort_blueprints ([blueprint_0 , blueprint_1 , blueprint_2 ])
121
+
122
+ # Then
123
+ assert result == [blueprint_2 , blueprint_0 , blueprint_1 ]
124
+
125
+
126
+ def test_sort_blueprints__does_not_sort_by_revision_when_revision_is_not_an_int ():
127
+ # Given
128
+ blueprint_0 = dict (name = "great-app" , version = "1.0.0" , summary = "summary" , revision = "c" )
129
+ blueprint_1 = dict (name = "great-app" , version = "1.0.0" , summary = "summary" , revision = "b" )
130
+ blueprint_2 = dict (name = "great-app" , version = "1.0.0" , summary = "summary" , revision = "a" )
131
+
132
+ # When
133
+ result = sort_blueprints ([blueprint_0 , blueprint_1 , blueprint_2 ])
134
+
135
+ # Then
136
+ assert result == [blueprint_0 , blueprint_1 , blueprint_2 ]
0 commit comments