Exemplo n.º 1
0
def test_search():
    with tempfile.TemporaryDirectory() as temp_dir:
        path = "/test/path/split"
        assert Search.split_path(path) == ["/", "test", "path", "split"]
        path = "test/path/split"
        assert Search.split_path(path) == ["test", "path", "split"]

        test_directory_fullpath = os.path.join(temp_dir, "test/directory")
        test_directory_1_fullpath = os.path.join(temp_dir, "test/directory/1")
        test_directory_2_fullpath = os.path.join(temp_dir, "test/directory/2")
        test_filename_fullpath = os.path.join(test_directory_fullpath,
                                              "node-0")
        test_filename_1_fullpath = os.path.join(test_directory_1_fullpath,
                                                "node-1")
        test_filename_2_fullpath = os.path.join(test_directory_2_fullpath,
                                                "node-2")
        os.makedirs(test_directory_1_fullpath)
        os.makedirs(test_directory_2_fullpath)
        open(test_filename_fullpath, "w").close()
        open(test_filename_1_fullpath, "w").close()
        open(test_filename_2_fullpath, "w").close()

        assert os.path.exists(test_filename_fullpath)
        assert os.path.exists(test_filename_1_fullpath)
        assert os.path.exists(test_filename_2_fullpath)

        with pytest.raises(ValueError):
            ContextSearch(os.path.relpath(temp_dir, test_directory_fullpath))

        with pytest.raises(ValueError):
            ContextSearch(test_filename_fullpath)

        with pytest.raises(ValueError):
            ContextSearch("/directory/doesnt/exist")

        assert (Search.exists(test_directory_fullpath,
                              "node-0") == test_filename_fullpath)
        assert (ContextSearch(test_directory_fullpath).exists("node-0") ==
                test_filename_fullpath)
        assert (Search.exists(test_directory_1_fullpath,
                              "node-1") == test_filename_1_fullpath)
        assert (ContextSearch(test_directory_1_fullpath).exists("node-1") ==
                test_filename_1_fullpath)
        assert (Search.exists(test_directory_2_fullpath,
                              "node-2") == test_filename_2_fullpath)
        assert (ContextSearch(test_directory_2_fullpath).exists("node-2") ==
                test_filename_2_fullpath)

        assert (Search.exists(test_directory_1_fullpath, "node-0",
                              up=1) == test_filename_fullpath)
        assert (ContextSearch(test_directory_1_fullpath).exists(
            "node-0", up=1) == test_filename_fullpath)
        assert (Search.exists(test_directory_2_fullpath, "node-0",
                              up=1) == test_filename_fullpath)
        assert (ContextSearch(test_directory_2_fullpath).exists(
            "node-0", up=1) == test_filename_fullpath)

        assert (Search.isfile(test_directory_fullpath,
                              "node-0") == test_filename_fullpath)
        assert (ContextSearch(test_directory_fullpath).isfile("node-0") ==
                test_filename_fullpath)

        assert (Search.isfile(test_directory_1_fullpath, "node-0",
                              up=1) == test_filename_fullpath)
        assert (ContextSearch(test_directory_1_fullpath).isfile(
            "node-0", up=1) == test_filename_fullpath)

        assert (Search.isdir(test_directory_fullpath,
                             os.path.basename(test_directory_1_fullpath)) ==
                test_directory_1_fullpath)
        assert (ContextSearch(test_directory_fullpath).isdir(
            os.path.basename(test_directory_1_fullpath)) ==
                test_directory_1_fullpath)
        assert (Search.isdir(
            test_directory_1_fullpath,
            os.path.basename(test_directory_fullpath),
            up=2,
        ) == test_directory_fullpath)
        assert (ContextSearch(test_directory_1_fullpath).isdir(
            os.path.basename(test_directory_fullpath),
            up=2) == test_directory_fullpath)

        assert (Search.upwards(test_directory_1_fullpath,
                               "node-0") == test_filename_fullpath)
        assert (ContextSearch(test_directory_2_fullpath).upwards("node-0") ==
                test_filename_fullpath)

        assert Search.downwards(temp_dir,
                                "node-1") == [test_filename_1_fullpath]
        assert ContextSearch(temp_dir).downwards("node-2") == [
            test_filename_2_fullpath
        ]

        assert Search.basename(test_directory_fullpath) == os.path.basename(
            test_directory_fullpath)
        assert ContextSearch(test_directory_fullpath).basename(
        ) == os.path.basename(test_directory_fullpath)

        assert Search.basename(
            test_directory_1_fullpath,
            up=1) == os.path.basename(test_directory_fullpath)
        assert ContextSearch(test_directory_2_fullpath).basename(
            up=1) == os.path.basename(test_directory_fullpath)

        assert Search.parent(test_directory_1_fullpath,
                             up=1) == test_directory_fullpath
        assert (ContextSearch(test_directory_2_fullpath).parent(
            up=1) == test_directory_fullpath)

        assert Search.parent(test_directory_1_fullpath, up=3) == temp_dir
        assert ContextSearch(test_directory_2_fullpath).parent(
            up=3) == temp_dir

        assert (Search.cut(
            "/p/re/fix/path/to/something/suf/fix",
            prefix="/p/re/fix",
            suffix="suf/fix",
        ) == "path/to/something")
        assert (Search.cut("/p/re/fix/path/to/something/suf/fix",
                           prefix="/p/re/fix") == "path/to/something/suf/fix")
        assert (Search.cut("/p/re/fix/path/to/something/suf/fix",
                           suffix="suf/fix") == "/p/re/fix/path/to/something")
Exemplo n.º 2
0
def test_search():
    with tempfile.TemporaryDirectory() as temp_dir:
        path = "/test/path/split"
        assert Search.split_path(path) == ['/', 'test', 'path', 'split']
        path = "test/path/split"
        assert Search.split_path(path) == ['test', 'path', 'split']

        test_directory_fullpath = os.path.join(temp_dir, 'test/directory')
        test_directory_1_fullpath = os.path.join(temp_dir, 'test/directory/1')
        test_directory_2_fullpath = os.path.join(temp_dir, 'test/directory/2')
        test_filename_fullpath = os.path.join(test_directory_fullpath,
                                              'node-0')
        test_filename_1_fullpath = os.path.join(test_directory_1_fullpath,
                                                'node-1')
        test_filename_2_fullpath = os.path.join(test_directory_2_fullpath,
                                                'node-2')
        os.makedirs(test_directory_1_fullpath)
        os.makedirs(test_directory_2_fullpath)
        os.mknod(test_filename_fullpath)
        os.mknod(test_filename_1_fullpath)
        os.mknod(test_filename_2_fullpath)

        assert os.path.exists(test_filename_fullpath)
        assert os.path.exists(test_filename_1_fullpath)
        assert os.path.exists(test_filename_2_fullpath)

        with pytest.raises(ValueError):
            ContextSearch(os.path.relpath(temp_dir, test_directory_fullpath))

        with pytest.raises(ValueError):
            ContextSearch(test_filename_fullpath)

        with pytest.raises(ValueError):
            ContextSearch("/directory/doesnt/exist")

        assert Search.exists(test_directory_fullpath,
                             'node-0') == test_filename_fullpath
        assert ContextSearch(test_directory_fullpath).exists(
            'node-0') == test_filename_fullpath
        assert Search.exists(test_directory_1_fullpath,
                             'node-1') == test_filename_1_fullpath
        assert ContextSearch(test_directory_1_fullpath).exists(
            'node-1') == test_filename_1_fullpath
        assert Search.exists(test_directory_2_fullpath,
                             'node-2') == test_filename_2_fullpath
        assert ContextSearch(test_directory_2_fullpath).exists(
            'node-2') == test_filename_2_fullpath

        assert Search.exists(test_directory_1_fullpath, 'node-0',
                             up=1) == test_filename_fullpath
        assert ContextSearch(test_directory_1_fullpath).exists(
            'node-0', up=1) == test_filename_fullpath
        assert Search.exists(test_directory_2_fullpath, 'node-0',
                             up=1) == test_filename_fullpath
        assert ContextSearch(test_directory_2_fullpath).exists(
            'node-0', up=1) == test_filename_fullpath

        assert Search.isfile(test_directory_fullpath,
                             'node-0') == test_filename_fullpath
        assert ContextSearch(test_directory_fullpath).isfile(
            'node-0') == test_filename_fullpath

        assert Search.isfile(test_directory_1_fullpath, 'node-0',
                             up=1) == test_filename_fullpath
        assert ContextSearch(test_directory_1_fullpath).isfile(
            'node-0', up=1) == test_filename_fullpath

        assert (Search.isdir(test_directory_fullpath,
                             os.path.basename(test_directory_1_fullpath)) ==
                test_directory_1_fullpath)
        assert (ContextSearch(test_directory_fullpath).isdir(
            os.path.basename(test_directory_1_fullpath)) ==
                test_directory_1_fullpath)
        assert (Search.isdir(test_directory_1_fullpath,
                             os.path.basename(test_directory_fullpath),
                             up=2) == test_directory_fullpath)
        assert (ContextSearch(test_directory_1_fullpath).isdir(
            os.path.basename(test_directory_fullpath),
            up=2) == test_directory_fullpath)

        assert Search.upwards(test_directory_1_fullpath,
                              'node-0') == test_filename_fullpath
        assert ContextSearch(test_directory_2_fullpath).upwards(
            'node-0') == test_filename_fullpath

        assert Search.downwards(temp_dir,
                                'node-1') == [test_filename_1_fullpath]
        assert ContextSearch(temp_dir).downwards('node-2') == [
            test_filename_2_fullpath
        ]

        assert Search.basename(test_directory_fullpath) == os.path.basename(
            test_directory_fullpath)
        assert ContextSearch(test_directory_fullpath).basename(
        ) == os.path.basename(test_directory_fullpath)

        assert Search.basename(
            test_directory_1_fullpath,
            up=1) == os.path.basename(test_directory_fullpath)
        assert ContextSearch(test_directory_2_fullpath).basename(
            up=1) == os.path.basename(test_directory_fullpath)

        assert Search.parent(test_directory_1_fullpath,
                             up=1) == test_directory_fullpath
        assert ContextSearch(test_directory_2_fullpath).parent(
            up=1) == test_directory_fullpath

        assert Search.parent(test_directory_1_fullpath, up=3) == temp_dir
        assert ContextSearch(test_directory_2_fullpath).parent(
            up=3) == temp_dir

        assert (Search.cut("/p/re/fix/path/to/something/suf/fix",
                           prefix="/p/re/fix",
                           suffix="suf/fix") == "path/to/something")
        assert (Search.cut("/p/re/fix/path/to/something/suf/fix",
                           prefix="/p/re/fix") == "path/to/something/suf/fix")
        assert (Search.cut("/p/re/fix/path/to/something/suf/fix",
                           suffix="suf/fix") == "/p/re/fix/path/to/something")