xref: /sqlite-3.40.0/test/shell8.test (revision 2f5f6740)
188be0209Sdan# 2017 December 9
288be0209Sdan#
388be0209Sdan# The author disclaims copyright to this source code.  In place of
488be0209Sdan# a legal notice, here is a blessing:
588be0209Sdan#
688be0209Sdan#    May you do good and not evil.
788be0209Sdan#    May you find forgiveness for yourself and forgive others.
888be0209Sdan#    May you share freely, never taking more than you give.
988be0209Sdan#
1088be0209Sdan#***********************************************************************
1188be0209Sdan#
1288be0209Sdan# Test the shell tool ".ar" command.
1388be0209Sdan#
1488be0209Sdan
1588be0209Sdanset testdir [file dirname $argv0]
1688be0209Sdansource $testdir/tester.tcl
1788be0209Sdanset testprefix shell8
18c48e0271Sdan
19c48e0271Sdanifcapable !vtab {
20c48e0271Sdan  finish_test; return
21c48e0271Sdan}
22*2f5f6740Slarrybrset CLI [test_cli_invocation]
2388be0209Sdan
24ded2d99dSdrh# Check to make sure the shell has been compiled with ".archive" support.
25ded2d99dSdrh#
26ded2d99dSdrhif {[string match {*unknown command*} [catchcmd :memory: .archive]]} {
27ded2d99dSdrh  finish_test; return
28ded2d99dSdrh}
29ded2d99dSdrh
3088be0209Sdanproc populate_dir {dirname spec} {
3188be0209Sdan  # First delete the current tree, if one exists.
3288be0209Sdan  file delete -force $dirname
3388be0209Sdan
3488be0209Sdan  # Recreate the root of the new tree.
3588be0209Sdan  file mkdir $dirname
3688be0209Sdan
3788be0209Sdan  # Add each file to the new tree.
3888be0209Sdan  foreach {f d} $spec {
3988be0209Sdan    set path [file join $dirname $f]
4088be0209Sdan    file mkdir [file dirname $path]
4188be0209Sdan    set fd [open $path w]
4288be0209Sdan    puts -nonewline $fd $d
4388be0209Sdan    close $fd
4488be0209Sdan  }
4588be0209Sdan}
4688be0209Sdan
477774fc01Slarrybrproc dir_content {dirname} {
487774fc01Slarrybr  lsort [glob -nocomplain $dirname/*]
497774fc01Slarrybr}
507774fc01Slarrybr
512ad0949dSdanproc dir_to_list {dirname {n -1}} {
522ad0949dSdan  if {$n<0} {set n [llength [file split $dirname]]}
532ad0949dSdan
5488be0209Sdan  set res [list]
5588be0209Sdan  foreach f [glob -nocomplain $dirname/*] {
5688be0209Sdan    set mtime [file mtime $f]
57dfdfd8c7Smistachkin    if {$::tcl_platform(platform)!="windows"} {
5888be0209Sdan      set perm [file attributes $f -perm]
59dfdfd8c7Smistachkin    } else {
60dfdfd8c7Smistachkin      set perm 0
61dfdfd8c7Smistachkin    }
622ad0949dSdan    set relpath [file join {*}[lrange [file split $f] $n end]]
6388be0209Sdan    lappend res
6488be0209Sdan    if {[file isdirectory $f]} {
6588be0209Sdan      lappend res [list $relpath / $mtime $perm]
6688be0209Sdan      lappend res {*}[dir_to_list $f]
6788be0209Sdan    } else {
6888be0209Sdan      set fd [open $f]
6988be0209Sdan      set data [read $fd]
7088be0209Sdan      close $fd
7188be0209Sdan      lappend res [list $relpath $data $mtime $perm]
7288be0209Sdan    }
7388be0209Sdan  }
7488be0209Sdan  lsort $res
7588be0209Sdan}
7688be0209Sdan
7788be0209Sdanproc dir_compare {d1 d2} {
7888be0209Sdan  set l1 [dir_to_list $d1]
7988be0209Sdan  set l2 [dir_to_list $d1]
8088be0209Sdan  string compare $l1 $l2
8188be0209Sdan}
8288be0209Sdan
83d4b56e59Sdanforeach {tn tcl} {
84d4b56e59Sdan  1 {
85d4b56e59Sdan    set c1 ".ar c ar1"
86d4b56e59Sdan    set x1 ".ar x"
87d4b56e59Sdan
88d4b56e59Sdan    set c2 ".ar cC ar1 ."
89d4b56e59Sdan    set x2 ".ar Cx ar3"
90ece4b0c1Sdan
91ece4b0c1Sdan    set c3 ".ar cCf ar1 test_xyz.db ."
92ece4b0c1Sdan    set x3 ".ar Cfx ar3 test_xyz.db"
93d4b56e59Sdan  }
94d4b56e59Sdan
95d4b56e59Sdan  2 {
96d4b56e59Sdan    set c1 ".ar -c ar1"
97d4b56e59Sdan    set x1 ".ar -x"
98d4b56e59Sdan
99d4b56e59Sdan    set c2 ".ar -cC ar1 ."
100d4b56e59Sdan    set x2 ".ar -xC ar3"
101ece4b0c1Sdan
102ece4b0c1Sdan    set c3 ".ar -cCar1 -ftest_xyz.db ."
103ece4b0c1Sdan    set x3 ".ar -x -C ar3 -f test_xyz.db"
104d4b56e59Sdan  }
105d4b56e59Sdan
106d4b56e59Sdan  3 {
107d4b56e59Sdan    set c1 ".ar --create ar1"
108d4b56e59Sdan    set x1 ".ar --extract"
109d4b56e59Sdan
110d4b56e59Sdan    set c2 ".ar --directory ar1 --create ."
111d4b56e59Sdan    set x2 ".ar --extract --dir ar3"
112ece4b0c1Sdan
113ece4b0c1Sdan    set c3 ".ar --creat --dir ar1 --file test_xyz.db ."
114a5676c4dSdrh    set x3 ".ar --e  --dir ar3 --f test_xyz.db"
115d4b56e59Sdan  }
116d4b56e59Sdan
117d4b56e59Sdan  4 {
118d4b56e59Sdan    set c1 ".ar --cr ar1"
119d4b56e59Sdan    set x1 ".ar --e"
120d4b56e59Sdan
121d4b56e59Sdan    set c2 ".ar -C ar1 -c ."
122d4b56e59Sdan    set x2 ".ar -x -C ar3"
123ece4b0c1Sdan
124ece4b0c1Sdan    set c3 ".ar -c --directory ar1 --file test_xyz.db ."
125ece4b0c1Sdan    set x3 ".ar -x --directory ar3 --file test_xyz.db"
126d4b56e59Sdan  }
127d4b56e59Sdan} {
128d4b56e59Sdan  eval $tcl
129d4b56e59Sdan
130d4b56e59Sdan  # Populate directory "ar1" with some files.
131d4b56e59Sdan  #
13288be0209Sdan  populate_dir ar1 {
13388be0209Sdan    file1 "abcd"
13488be0209Sdan    file2 "efgh"
13588be0209Sdan    dir1/file3 "ijkl"
13688be0209Sdan  }
13788be0209Sdan  set expected [dir_to_list ar1]
13888be0209Sdan
139d4b56e59Sdan  do_test 1.$tn.1 {
140d4b56e59Sdan    catchcmd test_ar.db $c1
14188be0209Sdan    file delete -force ar1
142d4b56e59Sdan    catchcmd test_ar.db $x1
14388be0209Sdan    dir_to_list ar1
14488be0209Sdan  } $expected
14588be0209Sdan
146d4b56e59Sdan  do_test 1.$tn.2 {
1472ad0949dSdan    file delete -force ar3
148d4b56e59Sdan    catchcmd test_ar.db $c2
149d4b56e59Sdan    catchcmd test_ar.db $x2
1501ad3f61bSdan    dir_to_list ar3
1511ad3f61bSdan  } $expected
152ece4b0c1Sdan
153ece4b0c1Sdan  do_test 1.$tn.3 {
154ece4b0c1Sdan    file delete -force ar3
155ece4b0c1Sdan    file delete -force test_xyz.db
156ece4b0c1Sdan    catchcmd ":memory:" $c3
157ece4b0c1Sdan    catchcmd ":memory:" $x3
158ece4b0c1Sdan    dir_to_list ar3
159ece4b0c1Sdan  } $expected
160ac15e2d7Sdan
161ac15e2d7Sdan  # This is a repeat of test 1.$tn.1, except that there is a 2 second
162ac15e2d7Sdan  # pause between creating the archive and extracting its contents.
163ac15e2d7Sdan  # This is to test that timestamps are set correctly.
164ac15e2d7Sdan  #
165ac15e2d7Sdan  # Because it is slow, only do this for $tn==1.
166ac15e2d7Sdan  if {$tn==1} {
167ac15e2d7Sdan    do_test 1.$tn.1 {
168ac15e2d7Sdan      catchcmd test_ar.db $c1
169ac15e2d7Sdan      file delete -force ar1
170ac15e2d7Sdan      after 2000
171ac15e2d7Sdan      catchcmd test_ar.db $x1
172ac15e2d7Sdan      dir_to_list ar1
173ac15e2d7Sdan    } $expected
174ac15e2d7Sdan  }
175d4b56e59Sdan}
176d4b56e59Sdan
1777774fc01Slarrybrdo_test 2.1.1 {
1787774fc01Slarrybr  populate_dir ar2 {
1797774fc01Slarrybr    file1 "abcd"
1807774fc01Slarrybr    file2 "efgh"
1818f09f4b2Slarrybr    junk1 "j1"
1828f09f4b2Slarrybr    junk2 "j2"
1837774fc01Slarrybr    dir1/file3 "ijkl"
1847774fc01Slarrybr  }
1857774fc01Slarrybr  populate_dir ar4 {
1867774fc01Slarrybr    file2 "efgh"
1877774fc01Slarrybr  }
1887774fc01Slarrybr  catchcmd shell8.db {.ar -c}
1897774fc01Slarrybr  catchcmd shell8.db {.ar -C ar2 -i .}
1907774fc01Slarrybr  catchcmd shell8.db {.ar -r ./file2 ./dir1}
1918f09f4b2Slarrybr  catchcmd shell8.db {.ar -g -r ./ju*2}
1927774fc01Slarrybr  catchcmd shell8.db {.ar -C ar4 -x .}
1937774fc01Slarrybr  regsub -all {ar4} [dir_content ar4] ar2
1948f09f4b2Slarrybr} {ar2/file1 ar2/file2 ar2/junk1}
19588be0209Sdan
19688be0209Sdanfinish_test
197