from homely._test import contents


def test_homely_general_include_executes_target_scripts(HOME, testrepo):
    from homely._test import run_update_all

    contents(
        testrepo.remotepath + '/HOMELY.py',
        """
        from homely.general import include
        include("test1/HOMELY.py")
        include("test2/HOMELY.py")
        """
    )

    contents(
        testrepo.remotepath + '/test1/HOMELY.py',
        """
        from homely.files import writefile
        with writefile("~/file1.txt") as f: f.write("I.\\n")
        """,
        mkdir=True,
    )

    contents(
        testrepo.remotepath + '/test2/HOMELY.py',
        """
        from homely.files import writefile
        with writefile("~/file2.txt") as f: f.write("II.\\n")
        """,
        mkdir=True,
    )

    run_update_all(
        # pull first so we get the new HOMELY.py script
        pullfirst=True,
    )

    assert contents(HOME + '/file1.txt') == "I.\n"
    assert contents(HOME + '/file2.txt') == "II.\n"


def test_homely_general_include_returns_target_modules(HOME, testrepo):
    from homely._test import run_update_all

    contents(
        testrepo.remotepath + '/HOMELY.py',
        """
        from homely.general import include

        mod1 = include("mod1/HOMELY.py")
        abc = include("a/b/c/HOMELY.py")

        assert mod1.THE_NUMBER == "Five"
        abc.do_something()
        """
    )

    contents(
        testrepo.remotepath + '/mod1/HOMELY.py',
        """
        THE_NUMBER = 'Five'
        """,
        mkdir=True,
    )

    contents(
        testrepo.remotepath + '/a/b/c/HOMELY.py',
        """
        from homely.files import writefile
        def do_something():
            with writefile("~/abc.txt") as f:
                f.write("Now I know my ABC's\\n")
        """,
        mkdir=True,
    )

    run_update_all(
        # pull first so we get the new HOMELY.py script
        pullfirst=True,
    )

    assert contents(HOME + '/abc.txt') == "Now I know my ABC's\n"


def test_homely_general_include_can_import_parent_module(HOME, testrepo):
    from homely._test import run_update_all

    contents(
        testrepo.remotepath + '/HOMELY.py',
        """
        from homely.general import include
        THE_WORD = "bonza"
        include("subdir/HOMELY.py")
        """
    )

    contents(
        testrepo.remotepath + '/subdir/HOMELY.py',
        """
        from homely.files import writefile
        import HOMELY
        with writefile('~/the_word.txt') as f: f.write(HOMELY.THE_WORD)
        """,
        mkdir=True,
    )

    run_update_all(
        # pull first so we get the new HOMELY.py script
        pullfirst=True,
    )

    assert contents(HOME + '/the_word.txt') == "bonza"

@@ -2726,16 +2726,1259 @@
 xt') == %22bonza%22%0A
+%0A%0Adef test_homely_general_include_is_always_relative_to_repo_root(HOME, testrepo):%0A    %22%22%22%0A    This test also ensures the main %22HOMELY%22 import is always the root module%0A    %22%22%22%0A    from homely._test import run_update_all%0A%0A    contents(%0A        testrepo.remotepath + '/HOMELY.py',%0A        %22%22%22%0A        from homely.general import include%0A        THE_WORD = 'flamingo'%0A        include(%22a/HOMELY.py%22)%0A        %22%22%22%0A    )%0A%0A    contents(%0A        testrepo.remotepath + '/a/HOMELY.py',%0A        %22%22%22%0A        THE_WORD = 'flamingo'%0A        from homely.general import include%0A        include(%22a/b/HOMELY.py%22)%0A        %22%22%22,%0A        mkdir=True,%0A    )%0A%0A    contents(%0A        testrepo.remotepath + '/a/b/HOMELY.py',%0A        %22%22%22%0A        from homely.general import include%0A        include(%22a/b/c/HOMELY.py%22)%0A        %22%22%22,%0A        mkdir=True,%0A    )%0A%0A    contents(%0A        testrepo.remotepath + '/a/b/c/HOMELY.py',%0A        %22%22%22%0A        from homely.files import writefile%0A        import HOMELY%0A        with writefile('~/the_word.txt') as f: f.write(HOMELY.THE_WORD)%0A        %22%22%22,%0A        mkdir=True,%0A    )%0A%0A    run_update_all(%0A        # pull first so we get the new HOMELY.py script%0A        pullfirst=True,%0A    )%0A%0A    assert contents(HOME + '/the_word.txt') == %22flamingo%22%0A

Test for nested includes()