|
11 | 11 | from typing import Optional |
12 | 12 |
|
13 | 13 | from ..api import usort, usort_path |
14 | | -from ..config import Config |
| 14 | +from ..config import CAT_FIRST_PARTY, Config |
15 | 15 | from ..translate import import_from_node |
16 | 16 | from ..util import parse_import |
17 | 17 |
|
@@ -1204,6 +1204,124 @@ def test_preserve_inline_comments_disabled(self) -> None: |
1204 | 1204 | config, |
1205 | 1205 | ) |
1206 | 1206 |
|
| 1207 | + def test_collapse_blank_lines_in_category_enabled(self) -> None: |
| 1208 | + """Test with collapse_blank_lines_in_category=True (default, post-commit 58c01556 behavior)""" |
| 1209 | + config = replace(DEFAULT_CONFIG, collapse_blank_lines_in_category=True) |
| 1210 | + self.assertUsortResult( |
| 1211 | + """ |
| 1212 | + import math |
| 1213 | +
|
| 1214 | + import gamma |
| 1215 | +
|
| 1216 | + import alpha |
| 1217 | +
|
| 1218 | +
|
| 1219 | + # special |
| 1220 | + import beta |
| 1221 | +
|
| 1222 | + from . import foo |
| 1223 | +
|
| 1224 | + import zeta |
| 1225 | +
|
| 1226 | + """, |
| 1227 | + """ |
| 1228 | + import math |
| 1229 | +
|
| 1230 | + import alpha |
| 1231 | + # special |
| 1232 | + import beta |
| 1233 | + import gamma |
| 1234 | + import zeta |
| 1235 | +
|
| 1236 | + from . import foo |
| 1237 | +
|
| 1238 | + """, |
| 1239 | + config, |
| 1240 | + ) |
| 1241 | + |
| 1242 | + def test_collapse_blank_lines_in_category_disabled(self) -> None: |
| 1243 | + """Test with collapse_blank_lines_in_category=False (pre-commit 58c01556 behavior)""" |
| 1244 | + config = replace(DEFAULT_CONFIG, collapse_blank_lines_in_category=False) |
| 1245 | + self.assertUsortResult( |
| 1246 | + """ |
| 1247 | + import math |
| 1248 | +
|
| 1249 | + import gamma |
| 1250 | +
|
| 1251 | + import alpha |
| 1252 | +
|
| 1253 | +
|
| 1254 | + # special |
| 1255 | + import beta |
| 1256 | +
|
| 1257 | + from . import foo |
| 1258 | +
|
| 1259 | + import zeta |
| 1260 | +
|
| 1261 | + """, |
| 1262 | + """ |
| 1263 | + import math |
| 1264 | +
|
| 1265 | + import alpha |
| 1266 | +
|
| 1267 | + # special |
| 1268 | + import beta |
| 1269 | +
|
| 1270 | + import gamma |
| 1271 | +
|
| 1272 | + import zeta |
| 1273 | +
|
| 1274 | + from . import foo |
| 1275 | +
|
| 1276 | + """, |
| 1277 | + config, |
| 1278 | + ) |
| 1279 | + |
| 1280 | + def test_collapse_blank_lines_in_category_default(self) -> None: |
| 1281 | + # Configure torch as first_party so both imports are in the same category |
| 1282 | + known = DEFAULT_CONFIG.known.copy() |
| 1283 | + known["torch"] = CAT_FIRST_PARTY |
| 1284 | + config = replace( |
| 1285 | + DEFAULT_CONFIG, |
| 1286 | + collapse_blank_lines_in_category=True, |
| 1287 | + known=known, |
| 1288 | + ) |
| 1289 | + self.assertUsortResult( |
| 1290 | + """ |
| 1291 | + import torch |
| 1292 | +
|
| 1293 | + from .runner import get_nn_runners |
| 1294 | + """, |
| 1295 | + """ |
| 1296 | + import torch |
| 1297 | + from .runner import get_nn_runners |
| 1298 | + """, |
| 1299 | + config, |
| 1300 | + ) |
| 1301 | + |
| 1302 | + def test_collapse_blank_lines_in_category_false(self) -> None: |
| 1303 | + # Configure torch as first_party so both imports are in the same category |
| 1304 | + known = DEFAULT_CONFIG.known.copy() |
| 1305 | + known["torch"] = CAT_FIRST_PARTY |
| 1306 | + config = replace( |
| 1307 | + DEFAULT_CONFIG, |
| 1308 | + collapse_blank_lines_in_category=False, |
| 1309 | + known=known, |
| 1310 | + ) |
| 1311 | + self.assertUsortResult( |
| 1312 | + """ |
| 1313 | + import torch |
| 1314 | +
|
| 1315 | + from .runner import get_runners |
| 1316 | + """, |
| 1317 | + """ |
| 1318 | + import torch |
| 1319 | +
|
| 1320 | + from .runner import get_runners |
| 1321 | + """, |
| 1322 | + config, |
| 1323 | + ) |
| 1324 | + |
1207 | 1325 |
|
1208 | 1326 | if __name__ == "__main__": |
1209 | 1327 | unittest.main() |
0 commit comments