From 0f8eff147a1e6d6d1fa081e4738a97dd3c02e13d Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Fri, 25 Sep 2020 20:42:56 -0700 Subject: [PATCH] Add test case for #1499: single-line multi-line string comment confuses float-to-top --- tests/unit/test_regressions.py | 77 ++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/tests/unit/test_regressions.py b/tests/unit/test_regressions.py index 4869d444f..5c609d200 100644 --- a/tests/unit/test_regressions.py +++ b/tests/unit/test_regressions.py @@ -1081,3 +1081,80 @@ def generator_function(): from \\ """ assert isort.check_code(raise_from_at_file_end_ignored, show_diff=True) + + +def test_isort_float_to_top_correctly_identifies_single_line_comments_1499(): + """Test to ensure isort correctly handles the case where float to top is used + to push imports to the top and the top comment is a multiline type but only + one line. + See: https://github.com/PyCQA/isort/issues/1499 + """ + assert ( + isort.code( + '''#!/bin/bash +"""My comment""" +def foo(): + pass + +import a + +def bar(): + pass +''', + float_to_top=True, + ) + == ( + '''#!/bin/bash +"""My comment""" +import a + + +def foo(): + pass + + +def bar(): + pass +''' + ) + ) + assert ( + isort.code( + """#!/bin/bash +'''My comment''' +def foo(): + pass + +import a + +def bar(): + pass +""", + float_to_top=True, + ) + == ( + """#!/bin/bash +'''My comment''' +import a + + +def foo(): + pass + + +def bar(): + pass +""" + ) + ) + + assert isort.check_code( + """#!/bin/bash +'''My comment''' +import a + +x = 1 +""", + float_to_top=True, + show_diff=True, + )