11from __future__ import annotations
22
3+ from copy import deepcopy
4+
5+ import pytest
6+
7+ from cleo .io .buffered_io import BufferedIO
8+
39from poetry .config .config_source import UNSET
410from poetry .config .config_source import ConfigSourceMigration
511from poetry .config .dict_config_source import DictConfigSource
612
713
8- def test_config_source_migration_rename_key () -> None :
9- config_data = {
14+ @pytest .fixture
15+ def config_source () -> DictConfigSource :
16+ config_source = DictConfigSource ()
17+ config_source ._config = {
1018 "virtualenvs" : {
1119 "prefer-active-python" : True ,
1220 },
1321 "system-git-client" : True ,
1422 }
1523
16- config_source = DictConfigSource ()
17- config_source . _config = config_data
24+ return config_source
25+
1826
27+ def test_config_source_migration_rename_key (config_source : DictConfigSource ) -> None :
1928 migration = ConfigSourceMigration (
2029 old_key = "virtualenvs.prefer-active-python" ,
2130 new_key = "virtualenvs.use-poetry-python" ,
2231 )
2332
2433 migration .apply (config_source )
2534
26- assert config_source ._config == {
35+ assert config_source .config == {
2736 "virtualenvs" : {
2837 "use-poetry-python" : True ,
2938 },
3039 "system-git-client" : True ,
3140 }
3241
3342
34- def test_config_source_migration_remove_key () -> None :
35- config_data = {
36- "virtualenvs" : {
37- "prefer-active-python" : True ,
38- },
39- "system-git-client" : True ,
40- }
41-
42- config_source = DictConfigSource ()
43- config_source ._config = config_data
44-
43+ def test_config_source_migration_remove_key (config_source : DictConfigSource ) -> None :
4544 migration = ConfigSourceMigration (
4645 old_key = "virtualenvs.prefer-active-python" ,
4746 new_key = None ,
4847 )
4948
5049 migration .apply (config_source )
5150
52- assert config_source ._config == {
51+ assert config_source .config == {
5352 "virtualenvs" : {},
5453 "system-git-client" : True ,
5554 }
5655
5756
58- def test_config_source_migration_unset_value () -> None :
59- config_data = {
60- "virtualenvs" : {
61- "prefer-active-python" : True ,
62- },
63- "system-git-client" : True ,
64- }
65-
66- config_source = DictConfigSource ()
67- config_source ._config = config_data
68-
57+ def test_config_source_migration_unset_value (config_source : DictConfigSource ) -> None :
6958 migration = ConfigSourceMigration (
7059 old_key = "virtualenvs.prefer-active-python" ,
7160 new_key = "virtualenvs.use-poetry-python" ,
@@ -74,23 +63,15 @@ def test_config_source_migration_unset_value() -> None:
7463
7564 migration .apply (config_source )
7665
77- assert config_source ._config == {
66+ assert config_source .config == {
7867 "virtualenvs" : {},
7968 "system-git-client" : True ,
8069 }
8170
8271
83- def test_config_source_migration_complex_migration () -> None :
84- config_data = {
85- "virtualenvs" : {
86- "prefer-active-python" : True ,
87- },
88- "system-git-client" : True ,
89- }
90-
91- config_source = DictConfigSource ()
92- config_source ._config = config_data
93-
72+ def test_config_source_migration_complex_migration (
73+ config_source : DictConfigSource ,
74+ ) -> None :
9475 migration = ConfigSourceMigration (
9576 old_key = "virtualenvs.prefer-active-python" ,
9677 new_key = "virtualenvs.use-poetry-python" ,
@@ -99,9 +80,71 @@ def test_config_source_migration_complex_migration() -> None:
9980
10081 migration .apply (config_source )
10182
102- assert config_source ._config == {
83+ assert config_source .config == {
10384 "virtualenvs" : {
10485 "use-poetry-python" : None ,
10586 },
10687 "system-git-client" : True ,
10788 }
89+
90+
91+ @pytest .mark .parametrize (
92+ ("migration" , "expected_result" , "expected_output" ),
93+ [
94+ pytest .param (
95+ ConfigSourceMigration (
96+ old_key = "virtualenvs.prefer-active-python" ,
97+ new_key = "virtualenvs.use-poetry-python" ,
98+ ),
99+ True ,
100+ (
101+ "virtualenvs.prefer-active-python = true -> "
102+ "virtualenvs.use-poetry-python = true"
103+ ),
104+ id = "rename-key" ,
105+ ),
106+ pytest .param (
107+ ConfigSourceMigration (
108+ old_key = "virtualenvs.prefer-active-python" ,
109+ new_key = None ,
110+ ),
111+ True ,
112+ "virtualenvs.prefer-active-python = true -> Removed from config" ,
113+ id = "remove-key" ,
114+ ),
115+ pytest .param (
116+ ConfigSourceMigration (
117+ old_key = "virtualenvs.prefer-active-python" ,
118+ new_key = "virtualenvs.use-poetry-python" ,
119+ value_migration = {True : UNSET , False : True },
120+ ),
121+ True ,
122+ (
123+ "virtualenvs.prefer-active-python = true -> "
124+ "virtualenvs.use-poetry-python = Not explicit set"
125+ ),
126+ id = "unset-value" ,
127+ ),
128+ pytest .param (
129+ ConfigSourceMigration (
130+ old_key = "virtualenvs.missing-key" ,
131+ new_key = "virtualenvs.use-poetry-python" ,
132+ ),
133+ False ,
134+ "" ,
135+ id = "missing-key" ,
136+ ),
137+ ],
138+ )
139+ def test_config_source_migration_dry_run (
140+ config_source : DictConfigSource ,
141+ migration : ConfigSourceMigration ,
142+ expected_result : bool ,
143+ expected_output : str ,
144+ ) -> None :
145+ original_config = deepcopy (config_source .config )
146+ io = BufferedIO ()
147+
148+ assert migration .dry_run (config_source , io ) is expected_result
149+ assert io .fetch_output ().strip () == expected_output
150+ assert config_source .config == original_config
0 commit comments