# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

package require tcltest 2
namespace import tcltest::*

set pwd [file dirname [file normalize $argv0]]

source ../port_test_autoconf.tcl
package require macports 1.0

array set ui_options {}
#set ui_options(ports_debug)   yes
#set ui_options(ports_verbose) yes
mportinit ui_options

package require portactivate 1.0
package require registry 1.0
source ./library.tcl
macports_worker_init


set activate_start_fixture_setup {
    global prefix

    set _save_prefix ${prefix}
    file mkdir ${pwd}/tmpdir
    set prefix ${pwd}/tmpdir

    rename getuid _save_getuid
    rename geteuid _save_geteuid

    set fake_uid 0
    set fake_euid 0
    set elevateToRootCalls [list]

    proc getuid {} {
        global fake_uid
        return ${fake_uid}
    }
    proc geteuid {} {
        global fake_euid
        return ${fake_euid}
    }

    proc elevateToRoot {phase} {
        global elevateToRootCalls
        lappend elevateToRootCalls $phase
    }
}
set activate_start_fixture_cleanup {
    rename getuid ""
    rename geteuid ""

    rename _save_getuid getuid
    rename _save_geteuid geteuid

    file delete -force ${pwd}/tmpdir
}

test activate_start_elevate {
    Test portactivate::activate_start to make sure it elevates to root if necessary
} -setup $activate_start_fixture_setup -cleanup $activate_start_fixture_cleanup -body {
    # file writable $prefix is used to determine whether privilege escalation
    # is needed, so set prefix to a directory unwritable for this user
    set prefix /usr/bin
    set fake_uid 0
    set fake_euid 500

    portactivate::activate_start

    return ${elevateToRootCalls}
} -result [list "activate"] -errorOutput ""

test activate_start_noelevate {
    Test portactivate::activate_start to make sure that it does not elevate to root if it cannot
} -setup $activate_start_fixture_setup -cleanup $activate_start_fixture_cleanup -body {
    set fake_uid 500
    set fake_euid 500

    portactivate::activate_start

    return ${elevateToRootCalls}
} -result [list] -errorOutput ""


test activate_main {
    Activate main unit test.
} -constraints {
    root
} -setup {
    set destpath $pwd/work/destroot
    set portbuildpath $pwd
    set portdbpath $pwd/dbpath
    set portpath $pwd

    set mport [mportopen file://.]

    # set $version var
    set workername [ditem_key $mport workername]

    # portinstall setup
    interp alias {} _cd {} cd

    # hide all output. Deactivate this for debugging!
    set oldchannels [array get macports::channels]
    set macports::channels(msg)    {}
    set macports::channels(notice) {}

    if {[$workername eval eval_targets install] != 0} {
        return "FAIL: port install failed"
    }

} -body {
    if {[$workername eval eval_targets activate] != 0} {
        return "FAIL: cannot activate port after install"
    }
    if {[$workername eval eval_targets deactivate] != 0} {
        return "FAIL: cannot deactivate port"
    }
    if {[$workername eval eval_targets activate] != 0} {
        return "FAIL: cannot activate port after deactivate"
    }
    return "Port activate successful."

} -cleanup {
    if {[$workername eval eval_targets uninstall] != 0} {
        return "FAIL: uninstall failed"
    }
    if {[$workername eval eval_targets clean] != 0} {
        return "FAIL: clean failed"
    }
    array set macports::channels $oldchannels

    mportclose $mport
} -result "Port activate successful." -errorOutput ""


cleanupTests
