# Tests for revision control functions                      -*- tcl -*-
#----------------------------------------------------------------------
# $Revision$
#----------------------------------------------------------------------

# Overload exec during these tests
stub exec {args} {
    set cmd [lindex $args 0]
    switch -- $cmd {
        cleartool {
            # cleartool lshistory -short $filename
            # cleartool pwv -s
            # cleartool get -to $outfile $filerev
            # cleartool ls $::diff($top,RevFile)
            if {[lindex $args 1] eq "lshistory"} {
                return [join [list x@/Apa/Bepa/12 x@/Apa/Cepa/2 x@/Apa/22 x@/Apa] \n]
            }
            if {[lindex $args 1] eq "pwv"} {
                return $::ct_pwv
            }
            if {[lindex $args 1] eq "ls"} {
                return $::ct_ls
            }
            if {[lindex $args 1] eq "describe"} {
                return $::ct_describe
            }
            return
        }
        cvs {
            # cvs -z3 update -p ?-r rev? $filename > $outfile
            return
        }
        co {
            # co -p$rev $filename > $outfile
            return
        }
        svn {
            switch -- [lindex $args 1] {
                info {
                    return {
                        Last Changed Author: pspjuth
                        Last Changed Rev: 158
                        Last Changed Date: 2008-03-04 07:39:43 +0100
                    }
                }
                log {
                    return {
                        ------------------------------------------
                        r122 | pspjuth | 2008-02-19 13:58:14 +0100
                        ------------------------------------------
                        r117 | pspjuth | 2008-02-15 09:07:16 +0100
                        ------------------------------------------
                        r115 | pspjuth | 2008-02-14 11:11:07 +0100
                        ------------------------------------------
                    }
                }
            }
            return
        }
        default {
            eval _stub_exec $args
        }
    }
}

# Do not detect git
set ::auto_execs(git) ""

test rev-1.1 {
    ClearCase revisions
} -body {
    set ::ct_ls @@/Bepa/5
    eskil::rev::CT::ParseRevs filename 2
} -result {/Bepa/2}

test rev-1.2 {
    ClearCase revisions
} -body {
    set ::ct_ls @@/Bepa/5
    eskil::rev::CT::ParseRevs filename 22
} -result {/Bepa/22}

test rev-1.3 {
    ClearCase revisions
} -body {
    set ::ct_ls @@/Bepa/5
    eskil::rev::CT::ParseRevs filename Bepa/12
} -result {/Apa/Bepa/12}

test rev-1.4 {
    ClearCase revisions
} -body {
    set ::ct_ls @@/Bepa/5
    eskil::rev::CT::ParseRevs filename Cepa/2
} -result {/Apa/Cepa/2}

test rev-1.5 {
    ClearCase revisions
} -body {
    set ::ct_ls @@/Apa/Bepa/Cepa/7
    eskil::rev::CT::ParseRevs filename ../5
} -result {/Apa/Bepa/5}

test rev-1.6 {
    ClearCase revisions
} -body {
    set ::ct_ls @@/Apa/Bepa/12
    eskil::rev::CT::ParseRevs filename ""
} -result {/Apa/Bepa/12}

test rev-1.7 {
    ClearCase revisions
} -body {
    set ::ct_describe /Apa/Bepa/11
    eskil::rev::CT::ParseRevs filename -1
} -result {/Apa/Bepa/11}

test rev-1.8 {
    ClearCase revisions
} -body {
    set ::ct_ls @@/Apa/Bepa/7
    eskil::rev::CT::ParseRevs filename Apa/-3
} -result {/Apa/19}

test rev-1.9 {
    ClearCase revisions
} -body {
    set ::ct_ls @@/Apa/Cepa/7
    eskil::rev::CT::ParseRevs filename -5
} -result {/Apa/Cepa/0}

test rev-2.1 {
    Detecting, CVS
} -body {
    set f [tcltest::makeFile {} _rev2_1]
    file mkdir CVS
    detectRevSystem $f
} -cleanup {
    tcltest::removeFile {} _rev2_1
} -result {CVS}

test rev-2.2 {
    Detecting, CVS
} -body {
    set ::auto_execs(cvs) ""
    set f [tcltest::makeFile {} _rev2_2]
    detectRevSystem $f
} -cleanup {
    file delete -force CVS
    tcltest::removeFile {} _rev2_2
} -result {}

test rev-2.3 {
    Detecting, RCS
} -body {
    set f [tcltest::makeFile {} _rev2_3]
    file mkdir RCS
    set ch [open RCS/_rev2_3,v w]
    puts $ch x
    close $ch
    set ::auto_execs(rcs) "x"
    detectRevSystem $f
} -cleanup {
    tcltest::removeFile {} _rev2_3
} -result {RCS}

test rev-2.4 {
    Detecting, RCS
} -body {
    set f [tcltest::makeFile {} _rev2_4]
    set ::auto_execs(rcs) ""
    detectRevSystem $f
} -cleanup {
    tcltest::removeFile {} _rev2_4
    file delete -force RCS
} -result {}

test rev-2.5 {
    Detecting, CT
} -body {
    set f [tcltest::makeFile {} _rev2_5]
    set ::auto_execs(cleartool) "x"
    unset -nocomplain ::ct_pwv
    detectRevSystem $f
} -cleanup {
    tcltest::removeFile {} _rev2_5
} -result {}

test rev-2.6 {
    Detecting, CT
} -body {
    set f [tcltest::makeFile {} _rev2_6]
    set ::auto_execs(cleartool) "x"
    set ::ct_pwv "** NONE **"
    detectRevSystem $f
} -cleanup {
    tcltest::removeFile {} _rev2_6
} -result {}

test rev-2.7 {
    Detecting, CT
} -body {
    set f [tcltest::makeFile {} _rev2_7]
    set ::auto_execs(cleartool) "x"
    set ::ct_pwv "gurka"
    detectRevSystem $f
} -cleanup {
    tcltest::removeFile {} _rev2_7
} -result {CT}

test rev-2.8 {
    Detecting, GIT
} -body {
    set f [tcltest::makeFile {} _rev2_8]
    set ::auto_execs(git) "x"
    set apa [file isdirectory .git]
    if {!$apa} {
        file mkdir .git
    }
    set res [detectRevSystem $f]
    if {!$apa} {
        file delete .git
    }
    set ::auto_execs(git) ""
    set res
} -cleanup {
    tcltest::removeFile {} _rev2_8
} -result {GIT}

test rev-2.9 {
    Detecting, HG
} -body {
    set f [tcltest::makeFile {} _rev2_9]
    set ::auto_execs(hg) "x"
    set apa [file isdirectory .hg]
    if {!$apa} {
        file mkdir .hg
    }
    set res [detectRevSystem $f]
    if {!$apa} {
        file delete .hg
    }
    set res
} -cleanup {
    tcltest::removeFile {} _rev2_9
} -result {HG}

test rev-2.10 {
    Detecting, SVN
} -body {
    set f [tcltest::makeFile {} _rev2_10]
    set ::auto_execs(svn) "x"
    set apa [file isdirectory .svn]
    if {!$apa} {
        file mkdir .svn
    }
    set res [detectRevSystem $f]
    if {!$apa} {
        file delete .svn
    }
    set res
} -cleanup {
    tcltest::removeFile {} _rev2_10
} -result {SVN}

test rev-2.11 {
    Detecting, BZR
} -body {
    set f [tcltest::makeFile {} _rev2_11]
    set ::auto_execs(bzr) "x"
    set apa [file isdirectory .bzr]
    if {!$apa} {
        file mkdir .bzr
    }
    set res [detectRevSystem $f]
    if {!$apa} {
        file delete .bzr
    }
    set res
} -cleanup {
    tcltest::removeFile {} _rev2_11
} -result {BZR}

test rev-2.12 {
    Detecting, Fossil
} -body {
    set f [tcltest::makeFile {} _rev2_12]
    set ::auto_execs(fossil) "x"
    set apa [file exists _FOSSIL_]
    if {!$apa} {
        file mkdir _FOSSIL_
    }
    set res [detectRevSystem $f]
    if {!$apa} {
        file delete _FOSSIL_
    }
    set res
} -cleanup {
    tcltest::removeFile {} _rev2_12
} -result {FOSSIL}

test rev-3.1 {
    Subversion revisions
} -body {
    eskil::rev::SVN::ParseRevs filename -1
} -result {157}

test rev-3.2 {
    Subversion revisions
} -body {
    eskil::rev::SVN::ParseRevs filename -2
} -result {115}
