def test_find_and_replace_preserve_preceding(): """When the SPDX header is in the middle of the file, keep it there.""" spdx_info = SpdxInfo({"GPL-3.0-or-later"}, {"SPDX" "-FileCopyrightText: Mary Sue"}) text = cleandoc(""" # Hello, world! def foo(bar): return bar # spdx-FileCopyrightText: Jane Doe pass """).replace("spdx", "SPDX") expected = cleandoc(""" # Hello, world! def foo(bar): return bar # spdx-FileCopyrightText: Jane Doe # spdx-FileCopyrightText: Mary Sue # # spdx-License-Identifier: GPL-3.0-or-later pass """).replace("spdx", "SPDX") assert find_and_replace_header(text, spdx_info) == expected
def test_find_and_replace_keep_shebang(): """When encountering a shebang, keep it and put the REUSE header beneath it. """ spdx_info = SpdxInfo({"GPL-3.0-or-later"}, {"SPDX" "-FileCopyrightText: Mary Sue"}) text = cleandoc(""" #!/usr/bin/env python3 # spdx-FileCopyrightText: Jane Doe pass """).replace("spdx", "SPDX") expected = cleandoc(""" #!/usr/bin/env python3 # spdx-FileCopyrightText: Jane Doe # spdx-FileCopyrightText: Mary Sue # # spdx-License-Identifier: GPL-3.0-or-later pass """).replace("spdx", "SPDX") assert find_and_replace_header(text, spdx_info) == expected
def test_find_and_replace_keep_old_comment(): """When encountering a comment that does not contain copyright and licensing information, preserve it below the REUSE header. """ spdx_info = SpdxInfo( {"GPL-3.0-or-later"}, {"SPDX" "-FileCopyrightText: Mary Sue"} ) text = cleandoc( """ # Hello, world! pass """ ).replace("spdx", "SPDX") expected = cleandoc( """ # spdx-FileCopyrightText: Mary Sue # # spdx-License-Identifier: GPL-3.0-or-later # Hello, world! pass """ ).replace("spdx", "SPDX") assert find_and_replace_header(text, spdx_info) == expected
def test_find_and_replace_only_shebang(): """When the file only contains a shebang, keep it at the top of the file. """ spdx_info = SpdxInfo({"GPL-3.0-or-later"}, set()) text = cleandoc( """ #!/usr/bin/env python3 # Hello, world! pass """ ) expected = cleandoc( """ #!/usr/bin/env python3 # spdx-License-Identifier: GPL-3.0-or-later # Hello, world! pass """ ).replace("spdx", "SPDX") assert find_and_replace_header(text, spdx_info) == expected
def test_find_and_replace_separate_shebang(): """When the shebang is part of the same comment as the SPDX comment, separate the two. """ spdx_info = SpdxInfo({"GPL-3.0-or-later"}, set()) text = cleandoc( """ #!/usr/bin/env python3 #!nix-shell -p python3 # spdx-FileCopyrightText: Jane Doe pass """ ).replace("spdx", "SPDX") expected = cleandoc( """ #!/usr/bin/env python3 #!nix-shell -p python3 # spdx-FileCopyrightText: Jane Doe # # spdx-License-Identifier: GPL-3.0-or-later pass """ ).replace("spdx", "SPDX") assert find_and_replace_header(text, spdx_info) == expected
def test_find_and_replace_newline_before_header(): """In a scenario where the header is preceded by whitespace, remove the preceding whitespace. """ spdx_info = SpdxInfo( {"GPL-3.0-or-later"}, {"SPDX" "-FileCopyrightText: Mary Sue"} ) text = cleandoc( """ # spdx-FileCopyrightText: Jane Doe pass """ ).replace("spdx", "SPDX") text = "\n" + text expected = cleandoc( """ # spdx-FileCopyrightText: Jane Doe # spdx-FileCopyrightText: Mary Sue # # spdx-License-Identifier: GPL-3.0-or-later pass """ ).replace("spdx", "SPDX") assert find_and_replace_header(text, spdx_info) == expected
def test_find_and_replace_newline_before_header(): """In a scenario where the header is not the first character in the file, create a new header. It would be nice if this were handled more elegantly. """ spdx_info = SpdxInfo( set(["GPL-3.0-or-later"]), set(["SPDX" "-FileCopyrightText: Mary Sue"]) ) text = cleandoc( """ # spdx-FileCopyrightText: Jane Doe pass """ ).replace("spdx", "SPDX") text = "\n" + text expected = cleandoc( """ # spdx-FileCopyrightText: Mary Sue # # spdx-License-Identifier: GPL-3.0-or-later # spdx-FileCopyrightText: Jane Doe pass """ ).replace("spdx", "SPDX") assert find_and_replace_header(text, spdx_info) == expected
def test_find_and_replace_verbatim(): """Replace a header with itself.""" spdx_info = SpdxInfo(set(), set()) text = cleandoc(""" # spdx-FileCopyrightText: Mary Sue # # spdx-License-Identifier: GPL-3.0-or-later pass """).replace("spdx", "SPDX") assert find_and_replace_header(text, spdx_info) == text
def test_find_and_replace_preserve_newline(): """If the file content ends with a newline, don't remove it.""" spdx_info = SpdxInfo(set(), set()) text = (cleandoc(""" # spdx-FileCopyrightText: Mary Sue # # spdx-License-Identifier: GPL-3.0-or-later pass """).replace("spdx", "SPDX") + "\n") assert find_and_replace_header(text, spdx_info) == text
def test_find_and_replace_no_header(): """Given text without header, add a header.""" spdx_info = SpdxInfo({"GPL-3.0-or-later"}, {"SPDX" "-FileCopyrightText: Mary Sue"}) text = "pass" expected = cleandoc(""" # spdx-FileCopyrightText: Mary Sue # # spdx-License-Identifier: GPL-3.0-or-later pass """).replace("spdx", "SPDX") assert find_and_replace_header(text, spdx_info) == expected