def test_publish_loops_through_all_resources():
    FAKE_WEB_ROOT = 'test_web_root'
    FILE_PATH_1 = 'some_path_1'
    FILE_PATH_2 = 'some_path_2'
    FILE_CONTENT = 'some_content'

    FILE_RELATIVE_PATH_1 = os.path.join(FAKE_WEB_ROOT, FILE_PATH_1)
    FILE_RELATIVE_PATH_2 = os.path.join(FAKE_WEB_ROOT, FILE_PATH_2)

    settings = CustomSettings(WEB_ROOT=FAKE_WEB_ROOT)

    not os.path.exists(FILE_RELATIVE_PATH_1) or os.remove(FILE_RELATIVE_PATH_1)
    not os.path.exists(FILE_RELATIVE_PATH_2) or os.remove(FILE_RELATIVE_PATH_2)

    try:
        with remove_web_root_from_settings():
            get_content_from_path = StaticGenerator.get_content_from_path
            StaticGenerator.get_content_from_path = lambda self, path: FILE_CONTENT
            instance = StaticGenerator(
                FILE_PATH_1, FILE_PATH_2,
                settings=settings,
            )

            instance.publish()

            with open(FILE_RELATIVE_PATH_1, 'r') as fd1, open(FILE_RELATIVE_PATH_2, 'r') as fd2:
                assert fd1.readline() == FILE_CONTENT, 'File {file_path} content differs'.format(file_path=FILE_RELATIVE_PATH_1)
                assert fd2.readline() == FILE_CONTENT, 'File {file_path} content differs'.format(file_path=FILE_RELATIVE_PATH_2)

    finally:
        StaticGenerator.get_content_from_path = get_content_from_path
def test_publish_loops_through_all_resources():
    mox = Mox()
    http_request, model_base, manager, model, queryset = get_mocks(mox)

    fs_mock = mox.CreateMockAnything()
    f = mox.CreateMockAnything()
    fs_mock.join("test_web_root", "some_path_1").AndReturn("test_web_root/some_path_1")
    fs_mock.dirname("test_web_root/some_path_1").AndReturn("test_web_root")
    fs_mock.exists("test_web_root").AndReturn(True)
    filename = "some_temp_file"
    fs_mock.tempfile(directory="test_web_root").AndReturn([f, filename])
    fs_mock.write(f, "some_content")
    fs_mock.close(f)
    fs_mock.chmod(
        filename,
        stat.S_IREAD | stat.S_IWRITE | stat.S_IWUSR | stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH,
    )
    fs_mock.rename("some_temp_file", "test_web_root/some_path_1")

    fs_mock.join("test_web_root", "some_path_2").AndReturn("test_web_root/some_path_2")
    fs_mock.dirname("test_web_root/some_path_2").AndReturn("test_web_root")
    fs_mock.exists("test_web_root").AndReturn(True)
    filename = "some_temp_file"
    fs_mock.tempfile(directory="test_web_root").AndReturn([f, filename])
    fs_mock.write(f, "some_content")
    fs_mock.close(f)
    fs_mock.chmod(
        filename,
        stat.S_IREAD | stat.S_IWRITE | stat.S_IWUSR | stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH,
    )
    fs_mock.rename("some_temp_file", "test_web_root/some_path_2")

    settings = CustomSettings(WEB_ROOT="test_web_root")

    mox.ReplayAll()

    try:
        get_content_from_path = StaticGenerator.get_content_from_path
        StaticGenerator.get_content_from_path = lambda self, path: "some_content"
        instance = StaticGenerator(
            "some_path_1",
            "some_path_2",
            http_request=http_request,
            model_base=model_base,
            manager=manager,
            model=model,
            queryset=queryset,
            settings=settings,
            fs=fs_mock,
        )

        instance.publish()

        mox.VerifyAll()
    finally:
        StaticGenerator.get_content_from_path = get_content_from_path
def test_publish_loops_through_all_resources():
    mox = Mox()
    http_request, model_base, manager, model, queryset = get_mocks(mox)

    fs_mock = mox.CreateMockAnything()
    f = mox.CreateMockAnything()
    fs_mock.join('test_web_root',
                 'some_path_1').AndReturn('test_web_root/some_path_1')
    fs_mock.dirname('test_web_root/some_path_1').AndReturn('test_web_root')
    fs_mock.exists("test_web_root").AndReturn(True)
    filename = "some_temp_file"
    fs_mock.tempfile(directory="test_web_root").AndReturn([f, filename])
    fs_mock.write(f, "some_content")
    fs_mock.close(f)
    fs_mock.chmod(
        filename, stat.S_IREAD | stat.S_IWRITE | stat.S_IWUSR | stat.S_IRUSR
        | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH)
    fs_mock.rename('some_temp_file', 'test_web_root/some_path_1')

    fs_mock.join('test_web_root',
                 'some_path_2').AndReturn('test_web_root/some_path_2')
    fs_mock.dirname('test_web_root/some_path_2').AndReturn('test_web_root')
    fs_mock.exists("test_web_root").AndReturn(True)
    filename = "some_temp_file"
    fs_mock.tempfile(directory="test_web_root").AndReturn([f, filename])
    fs_mock.write(f, "some_content")
    fs_mock.close(f)
    fs_mock.chmod(
        filename, stat.S_IREAD | stat.S_IWRITE | stat.S_IWUSR | stat.S_IRUSR
        | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH)
    fs_mock.rename('some_temp_file', 'test_web_root/some_path_2')

    settings = CustomSettings(WEB_ROOT="test_web_root")

    mox.ReplayAll()

    try:
        get_content_from_path = StaticGenerator.get_content_from_path
        StaticGenerator.get_content_from_path = lambda self, path: "some_content"
        instance = StaticGenerator("some_path_1",
                                   "some_path_2",
                                   http_request=http_request,
                                   model_base=model_base,
                                   manager=manager,
                                   model=model,
                                   queryset=queryset,
                                   settings=settings,
                                   fs=fs_mock)

        instance.publish()

        mox.VerifyAll()
    finally:
        StaticGenerator.get_content_from_path = get_content_from_path
예제 #4
0
def test_publish_loops_through_all_resources():
    FAKE_WEB_ROOT = 'test_web_root'
    FILE_PATH_1 = 'some_path_1'
    FILE_PATH_2 = 'some_path_2'
    FILE_CONTENT = 'some_content'

    FILE_RELATIVE_PATH_1 = os.path.join(FAKE_WEB_ROOT, FILE_PATH_1)
    FILE_RELATIVE_PATH_2 = os.path.join(FAKE_WEB_ROOT, FILE_PATH_2)

    settings = CustomSettings(WEB_ROOT=FAKE_WEB_ROOT)

    not os.path.exists(FILE_RELATIVE_PATH_1) or os.remove(FILE_RELATIVE_PATH_1)
    not os.path.exists(FILE_RELATIVE_PATH_2) or os.remove(FILE_RELATIVE_PATH_2)

    try:
        with remove_web_root_from_settings():
            get_content_from_path = StaticGenerator.get_content_from_path
            StaticGenerator.get_content_from_path = lambda self, path: FILE_CONTENT
            instance = StaticGenerator(
                FILE_PATH_1,
                FILE_PATH_2,
                settings=settings,
            )

            instance.publish()

            with open(FILE_RELATIVE_PATH_1,
                      'r') as fd1, open(FILE_RELATIVE_PATH_2, 'r') as fd2:
                assert fd1.readline(
                ) == FILE_CONTENT, 'File {file_path} content differs'.format(
                    file_path=FILE_RELATIVE_PATH_1)
                assert fd2.readline(
                ) == FILE_CONTENT, 'File {file_path} content differs'.format(
                    file_path=FILE_RELATIVE_PATH_2)

    finally:
        StaticGenerator.get_content_from_path = get_content_from_path