@@ -79,6 +79,7 @@ def test_parse_args():
79
79
assert main .parse_args (["--dont-follow-links" ]) == {"follow_links" : False }
80
80
assert main .parse_args (["--overwrite-in-place" ]) == {"overwrite_in_place" : True }
81
81
assert main .parse_args (["--from-first" ]) == {"from_first" : True }
82
+ assert main .parse_args (["--resolve-all-configs" ]) == {"resolve_all_configs" : True }
82
83
83
84
84
85
def test_ascii_art (capsys ):
@@ -1211,3 +1212,136 @@ def main_check(args):
1211
1212
out , error = main_check ([str (git_project0 ), "--skip-gitignore" , "--filter-files" ])
1212
1213
1213
1214
assert all (f"{ str (tmpdir )} { file } " in out for file in should_check )
1215
+
1216
+
1217
+ def test_multiple_configs (capsys , tmpdir ):
1218
+ # Ensure that --resolve-all-configs flag resolves multiple configs correctly
1219
+ # and sorts files corresponding to their nearest config
1220
+
1221
+ setup_cfg = """
1222
+ [isort]
1223
+ from_first=True
1224
+ """
1225
+
1226
+ pyproject_toml = """
1227
+ [tool.isort]
1228
+ no_inline_sort = \" True\"
1229
+ """
1230
+
1231
+ isort_cfg = """
1232
+ [settings]
1233
+ force_single_line=True
1234
+ """
1235
+
1236
+ broken_isort_cfg = """
1237
+ [iaort_confg]
1238
+ force_single_line=True
1239
+ """
1240
+
1241
+ dir1 = tmpdir / "subdir1"
1242
+ dir2 = tmpdir / "subdir2"
1243
+ dir3 = tmpdir / "subdir3"
1244
+ dir4 = tmpdir / "subdir4"
1245
+
1246
+ dir1 .mkdir ()
1247
+ dir2 .mkdir ()
1248
+ dir3 .mkdir ()
1249
+ dir4 .mkdir ()
1250
+
1251
+ setup_cfg_file = dir1 / "setup.cfg"
1252
+ setup_cfg_file .write_text (setup_cfg , "utf-8" )
1253
+
1254
+ pyproject_toml_file = dir2 / "pyproject.toml"
1255
+ pyproject_toml_file .write_text (pyproject_toml , "utf-8" )
1256
+
1257
+ isort_cfg_file = dir3 / ".isort.cfg"
1258
+ isort_cfg_file .write_text (isort_cfg , "utf-8" )
1259
+
1260
+ broken_isort_cfg_file = dir4 / ".isort.cfg"
1261
+ broken_isort_cfg_file .write_text (broken_isort_cfg , "utf-8" )
1262
+
1263
+ import_section = """
1264
+ from a import y, z, x
1265
+ import b
1266
+ """
1267
+
1268
+ file1 = dir1 / "file1.py"
1269
+ file1 .write_text (import_section , "utf-8" )
1270
+
1271
+ file2 = dir2 / "file2.py"
1272
+ file2 .write_text (import_section , "utf-8" )
1273
+
1274
+ file3 = dir3 / "file3.py"
1275
+ file3 .write_text (import_section , "utf-8" )
1276
+
1277
+ file4 = dir4 / "file4.py"
1278
+ file4 .write_text (import_section , "utf-8" )
1279
+
1280
+ file5 = tmpdir / "file5.py"
1281
+ file5 .write_text (import_section , "utf-8" )
1282
+
1283
+ main .main ([str (tmpdir ), "--resolve-all-configs" , "--cr" , str (tmpdir ), "--verbose" ])
1284
+ out , _ = capsys .readouterr ()
1285
+
1286
+ assert f"{ str (setup_cfg_file )} used for file { str (file1 )} " in out
1287
+ assert f"{ str (pyproject_toml_file )} used for file { str (file2 )} " in out
1288
+ assert f"{ str (isort_cfg_file )} used for file { str (file3 )} " in out
1289
+ assert f"default used for file { str (file4 )} " in out
1290
+ assert f"default used for file { str (file5 )} " in out
1291
+
1292
+ assert (
1293
+ file1 .read ()
1294
+ == """
1295
+ from a import x, y, z
1296
+ import b
1297
+ """
1298
+ )
1299
+
1300
+ assert (
1301
+ file2 .read ()
1302
+ == """
1303
+ import b
1304
+ from a import y, z, x
1305
+ """
1306
+ )
1307
+ assert (
1308
+ file3 .read ()
1309
+ == """
1310
+ import b
1311
+ from a import x
1312
+ from a import y
1313
+ from a import z
1314
+ """
1315
+ )
1316
+ assert (
1317
+ file4 .read ()
1318
+ == """
1319
+ import b
1320
+ from a import x, y, z
1321
+ """
1322
+ )
1323
+
1324
+ assert (
1325
+ file5 .read ()
1326
+ == """
1327
+ import b
1328
+ from a import x, y, z
1329
+ """
1330
+ )
1331
+
1332
+ # Ensure that --resolve-all-config flags works with --check
1333
+
1334
+ file6 = dir1 / "file6.py"
1335
+ file6 .write (
1336
+ """
1337
+ import b
1338
+ from a import x, y, z
1339
+ """
1340
+ )
1341
+
1342
+ with pytest .raises (SystemExit ):
1343
+ main .main ([str (tmpdir ), "--resolve-all-configs" , "--cr" , str (tmpdir ), "--check" ])
1344
+
1345
+ _ , err = capsys .readouterr ()
1346
+
1347
+ assert f"{ str (file6 )} Imports are incorrectly sorted and/or formatted" in err
0 commit comments