#
# Copyright (c) 2013 D. Richard Hipp
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the Simplified BSD License (also
# known as the "2-Clause License" or "FreeBSD License".)
#
# This program is distributed in the hope that it will be useful,
# but without any warranty; without even the implied warranty of
# merchantability or fitness for a particular purpose.
#
# Author contact information:
#   drh@hwaci.com
#   http://www.hwaci.com/drh/
#
############################################################################
#
#
# Tests for 'fossil revert'
#
#

# Test 'fossil revert' against expected results from 'fossil changes' and
# 'fossil addremove -n', as well as by verifying the existence of files
# on the file system. 'fossil undo' is called after each test
#
proc revert-test {testid revertArgs expectedRevertOutput args} {
  global RESULT
  set passed 1

  set args [dict merge {
    -changes {} -addremove {} -exists {} -notexists {}
  } $args]

  set result [fossil revert {*}$revertArgs]
  test_status_list revert-$testid $result $expectedRevertOutput

  set statusListTests [list -changes changes -addremove {addremove -n}]
  foreach {key fossilArgs} $statusListTests {
    set expected [dict get $args $key]
    set result [fossil {*}$fossilArgs]
    test_status_list revert-$testid$key $result $expected
  }

  set fileExistsTests [list -exists 1 does -notexists 0 should]
  foreach {key expected verb} $fileExistsTests {
    foreach path [dict get $args $key] {
      if {[file exists $path] != $expected} {
        set passed 0
        protOut "  Failure: File $verb not exist: $path"
      }
    }
    test revert-$testid$key $passed
  }

  fossil undo
}

require_no_open_checkout
test_setup

# Prepare first commit
#
write_file f1 "f1"
write_file f2 "f2"
write_file f3 "f3"
fossil add f1 f2 f3
fossil commit -m "c1"

# Make changes to be reverted
#
# Add f0
write_file f0 "f0"
fossil add f0
# Remove f1
file delete f1
fossil rm f1
# Edit f2
write_file f2 "f2.1"
# Rename f3 to f3n
file rename -force f3 f3n
fossil mv f3 f3n

# Test 'fossil revert' with no arguments
#
revert-test 1-1 {} {
  UNMANAGE f0
  REVERT   f1
  REVERT   f2
  REVERT   f3
  DELETE   f3n
} -addremove {
  ADDED f0
} -exists {f0 f1 f2 f3} -notexists f3n

# Test with a single filename argument
#
revert-test 1-2 f0 {
  UNMANAGE f0
} -changes {
  DELETED f1
  EDITED f2
  RENAMED f3  ->  f3n
} -addremove {
  ADDED f0
} -exists {f0 f2 f3n} -notexists f3

revert-test 1-3 f1 {
  REVERT   f1
} -changes {
  ADDED f0
  EDITED f2
  RENAMED f3  ->  f3n
} -exists {f0 f1 f2 f3n} -notexists f3

revert-test 1-4 f2 {
  REVERT   f2
} -changes {
  ADDED f0
  DELETED f1
  RENAMED f3  ->  f3n
} -exists {f0 f2 f3n} -notexists {f1 f3}

# Both files involved in a rename are reverted regardless of which filename
# is used as an argument to 'fossil revert'
#
revert-test 1-5 f3 {
  REVERT   f3
  DELETE   f3n
} -changes {
  ADDED f0
  DELETED f1
  EDITED f2
} -exists {f0 f2 f3} -notexists {f1 f3n}

revert-test 1-6 f3n {
  REVERT   f3
  DELETE   f3n
} -changes {
  ADDED f0
  DELETED f1
  EDITED f2
} -exists {f0 f2 f3} -notexists {f1 f3n}

# Test with multiple filename arguments
#
revert-test 1-7 {f0 f2 f3n} {
  UNMANAGE f0
  REVERT   f2
  REVERT   f3
  DELETE   f3n
} -changes {
  DELETED f1
} -addremove {
  ADDED f0
} -exists {f0 f2 f3} -notexists {f1 f3n}


# Test reverting the combination of a renamed file and an added file that
# uses the renamed file's original filename.
#
test_setup
write_file f1 "f1"
fossil add f1
fossil commit -m "add f1"

write_file f1n "f1n"
fossil mv f1 f1n
write_file f1 "f1b"
fossil add f1

revert-test 2-1 {} {
  REVERT   f1
  DELETE   f1n
} -exists {f1} -notexists {f1n}


# Test reverting a rename in the repo but not completed in the file
# system
test_setup
write_file f1 "f1"
fossil add f1
fossil commit -m "add f1"
fossil mv --soft f1 f1new
test 3-mv-1 {[file exists f1]}
test 3-mv-2 {![file exists f1new]}
revert-test 3-1 {} {
  REVERT f1
  DELETE f1new
} -exists {f1} -notexists {f1n}


# Test reverting of files under a sub-directory
test_setup
file mkdir d
write_file d/f1 "d/f1"
write_file d/f2 "d/f2"
write_file d/f3 "d/f3"
write_file d/f4 "d/f4"

fossil add d
fossil delete d/f1
fossil commit -m "d/f2 d/f3 d/f4"

## Changes to revert
fossil add d/f1
write_file d/f2 "4-1:d/f2"
fossil changes d/f2
fossil delete --soft d/f3

revert-test 4-1 {d/f1} {
  UNMANAGE   d/f1
} -changes {
  EDITED  d/f2
  DELETED d/f3
} -addremove {
  ADDED   d/f1
} -exists {d/f1 d/f2 d/f3}

revert-test 4-2 {d/f2} {
  REVERT  d/f2
} -changes {
  ADDED   d/f1
  DELETED d/f3
} -exists {d/f1 d/f2 d/f3}

revert-test 4-3 {d/f3} {
  REVERT   d/f3
} -changes {
  ADDED   d/f1
  EDITED  d/f2
} -exists {d/f1 d/f2 d/f3}

fossil mv --soft d/f4 d/f4new
test 4-4-mv-1 {[file exists d/f4]}
test 4-4-mv-2 {![file exists d/f4new]}
revert-test 4-4 {d/f4} {
  DELETE   d/f4new
  REVERT   d/f4
} -changes {
  ADDED   d/f1
  EDITED  d/f2
  DELETED d/f3
} -exists {d/f4} -notexists {d/f4new}

## Commit changes before testing reverting of directory rename,
## otherwise there're could be sequencing issues
fossil redo
fossil commit -m "4-5:setup"

fossil mv --soft d dnew
revert-test 4-5 {d/f1 d/f2 d/f3 d/f4} {
  REVERT   d/f1
  REVERT   d/f2
  UNMANAGE d/f3
  REVERT   d/f4
  DELETE   dnew/f1
  DELETE   dnew/f2
  DELETE   dnew/f4
} -addremove {
  ADDED    d/f3
} -exists {d/f1 d/f2 d/f3 d/f4} -notexists {dnew}


## Test reverting of changes in whole sub-directory tree
test_setup
file mkdir d
write_file f0 "f0"
write_file d/f1 "d/f1"
write_file d/f2 "d/f2"
write_file d/f3 "d/f3"
write_file d/f4 "d/f4"

fossil add f0 d
fossil delete d/f1
fossil commit -m "f0 d/f2 d/f3 d/f4"

## Changes to revert
fossil add d/f1
write_file d/f2 "5-1:d/f2"
fossil changes d/f2
fossil delete --soft d/f3

revert-test 5-1 {d} {
  UNMANAGE d/f1
  REVERT   d/f2
  REVERT   d/f3
} -addremove {
  ADDED    d/f1
} -exists {f0 d/f1 d/f2 d/f3}

write_file f0 "5-2:f0"
fossil changes f0
revert-test 5-2 {f0 d} {
  UNMANAGE d/f1
  REVERT   d/f2
  REVERT   d/f3
  REVERT   f0
} -addremove {
  ADDED    d/f1
} -exists {f0 d/f1 d/f2 d/f3}

## Commit changes before testing the revert of directory rename,
## otherwise there're could be sequencing issues
fossil commit -m "5-3:setup"

fossil changes

fossil mv --soft d dnew
revert-test 5-3 {d} {
  REVERT   d/f1
  REVERT   d/f2
  REVERT   d/f4
  DELETE   dnew/f1
  DELETE   dnew/f2
  DELETE   dnew/f4
} -addremove {
  ADDED    d/f3
} -exists {f0 d/f1 d/f2 d/f3 d/f4} -notexists {dnew}

## Reset/redo the undone results of revert to get to a clean checkout
fossil redo

file mkdir d/e
file mkdir d/e/f
write_file d/e/fe1 "d/e/fe1"
write_file d/e/f/ff1 "d/e/f/ff1"

file mkdir d1
file mkdir d1/e
write_file d1/e/fe1 "d1/e/fe1"
write_file d1/e/fe2 "d1/e/fe2"

fossil add d1/e/fe1
fossil commit d1/e/fe1 -m "d1/e/fe1"

write_file d1/e/fe1 "5-4:d1/e/fe1"
fossil changes d1/e/fe1
fossil add d d1

revert-test 5-4 {d d1} {
  UNMANAGE d/f3
  UNMANAGE d/e/fe1
  UNMANAGE d/e/f/ff1
  REVERT   d1/e/fe1
  UNMANAGE d1/e/fe2
} -addremove {
  ADDED    d/f3
  ADDED    d/e/fe1
  ADDED    d/e/f/ff1
  ADDED    d1/e/fe2
} -exists {d/f1 d/f2 d/f3 d/f4 d/e/fe1 d/e/fe1 d/e/f/ff1
           d1/e/fe1 d1/e/fe2}


###############################################################################

test_cleanup
