#!/usr/bin/env qore
# -*- mode: qore; indent-tabs-mode: nil -*-

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

%requires ../../../../qlib/Util.qm
%requires ../../../../qlib/QUnit.qm

%exec-class MkdirTest

class MkdirTest inherits QUnit::Test {
    public {}

    private {
        const dir_pos = "/mkpos";
        const dir_neg = "/mkneg/mk2/mk3/mk4";
        const dir_par = "/mkparpos3";
    }

    constructor() : Test("mkdir Test", "1.0") {
        addTestCase("mkdirPosTest", \mkdirPosTest());
        addTestCase("mkdirNegTest", \mkdirNegTest());
        addTestCase("mkdirParentTest", \mkdirParentTest());

        set_return_value(main());
    }

    mkdirPosTest() {
        string dir = tmp_location() + dir_pos;
        on_exit rmdir(dir);
        int ret = mkdir(dir);
        assertEq(0, ret);
    }

    mkdirNegTest() {
        string dir = tmp_location() + dir_neg;
        on_exit rmdir(dir);
        int ret = mkdir(dir);
        assertEq(-1, ret);

        assertEq(-1, mkdir("/err/invalid", 0700, True));
        assertThrows("DIR-CREATE-FAILURE", \mkdir_ex(), ("/err/invalid", 0700, True));
    }

    mkdirParentTest() {
        string dir1 = tmp_location() + dir_par;
        string dir2 = dir1 + DirSep + "mk2";
        string dir3 = dir2 + DirSep + "mk3";
        int ret = mkdir(dir3, 0777, True);
        on_exit {
            rmdir(dir3);
            rmdir(dir2);
            rmdir(dir1);
        }
        assertEq("DIRECTORY", hstat(dir3).type);
        assertEq("DIRECTORY", hstat(dir2).type);
        assertEq("DIRECTORY", hstat(dir1).type);

        assertEq(0, ret);
    }
}
