#!/usr/bin/env qore

%requires Util

%requires ../../../../qlib/FsUtil.qm
%requires ../../../../qlib/QUnit.qm

%new-style
%require-types
%enable-all-warnings

%exec-class CopyFileObjTest

public class CopyFileObjTest inherits QUnit::Test {

    constructor() : Test ("CopyFileObjTest", "1.0") {
        addTestCase("path_exists test", \test_path_exists());
        set_return_value(main());
    }

    test_path_exists() {
        # get a directory
        string tmp_dir = make_tmp_dir();
        assertTrue(path_exists(tmp_dir));
        assertFalse(path_exists(join_paths(tmp_dir, "non-existent.txt")));

        # get a file
        hash<TmpFileHash> tmp_file = make_tmp_file(NOTHING, NOTHING, tmp_dir);
        tmp_file.file.close();
        assertTrue(path_exists(tmp_file.path));

        # create a link to that file
        string tmp_link = join_paths(tmp_dir, "link");
        symlink(tmp_file.path, tmp_link);
        assertTrue(path_exists(tmp_link));

        # remove the file (but keep the link)
        unlink(tmp_file.path);
        assertFalse(path_exists(tmp_file.path));
        assertTrue(path_exists(tmp_link));
        assertFalse(path_exists(tmp_link, True));

        # remove the link
        unlink(tmp_link);
        assertFalse(path_exists(tmp_link));

        # remove the directory as well
        remove_tree(tmp_dir);
        assertFalse(path_exists(tmp_dir));
    }
}
