Browse Source

Add version sub-command

Alois Mahdal 6 years ago
parent
commit
e63422c5d8
2 changed files with 125 additions and 0 deletions
  1. 19
    0
      src/shellfu.sh.skel
  2. 106
    0
      tests/shellfu_version/TF_RUN

+ 19
- 0
src/shellfu.sh.skel View File

@@ -122,6 +122,25 @@ shellfu() {
122 122
             local mname=$1
123 123
             ( shellfu _do_import "$mname" )
124 124
             ;;
125
+        version)
126
+            #
127
+            # Show version of module named $1
128
+            #
129
+            local mname=$1
130
+            mpath=$(shellfu _select_mfile "$mname") || {
131
+                shellfu __warn "cannot find module: $mname"
132
+                return 3
133
+            }
134
+            shellfu __debug "found module file: $mpath"
135
+            tail -3 "$mpath" \
136
+              | grep '^#shellfu  *module-version:' \
137
+              | sed 's/.*: *//; s/ *$//' \
138
+              | grep . \
139
+             || {
140
+                shellfu __warn "missing version declaration: $mpath"
141
+                return 2
142
+            }
143
+            ;;
125 144
 
126 145
         _do_import)
127 146
             #

+ 106
- 0
tests/shellfu_version/TF_RUN View File

@@ -0,0 +1,106 @@
1
+#!/bin/bash
2
+
3
+. "$TF_DIR/include/subtest.sh"
4
+. "$TF_DIR/include/tools.sh"
5
+
6
+tf_enum_subtests() {
7
+    echo "no_module"
8
+    echo "empty_file"
9
+    echo "no_declaration"
10
+    echo "deep1"
11
+    echo "deep2"
12
+    echo "deep3"
13
+    echo "deep4"
14
+    echo "version_empty"
15
+    echo "version_spaces"
16
+}
17
+
18
+mkwrapper() {
19
+    #
20
+    # Prepare script body
21
+    #
22
+    local modname=$1
23
+    echo '#!/bin/bash'
24
+    echo 'SHELLFU_PATH=test/shellfu'
25
+    echo '. "$(shellfu-get path)" || exit 3'
26
+    echo "shellfu version $modname"
27
+}
28
+
29
+mkmodule() {
30
+    #
31
+    # Prepare module body
32
+    #
33
+    test "$name" == no_module && return 0
34
+    {
35
+        test "$name" == empty_file || {
36
+            echo 'foo() {'
37
+            echo '    echo hello'
38
+            echo '}'
39
+        }
40
+        case $name in
41
+            no_declaration)
42
+                :
43
+                ;;
44
+            deep?)
45
+                n=${name#deep}
46
+                echo '#shellfu module-version: 0.1.2'
47
+                while test "$n" -gt 1; do
48
+                    ((n--))
49
+                    echo
50
+                done
51
+                ;;
52
+            version_empty)
53
+                echo '#shellfu module-version:'
54
+                ;;
55
+            version_spaces)
56
+                echo '#shellfu    module-version:    0.1.2   '
57
+                ;;
58
+        esac
59
+    } > "test/shellfu/$name.sh"
60
+}
61
+
62
+mkoracle() {
63
+    case $name in
64
+        deep1)          echo 0.1.2 ;;
65
+        deep2)          echo 0.1.2 ;;
66
+        deep3)          echo 0.1.2 ;;
67
+        version_spaces) echo 0.1.2 ;;
68
+    esac > "oracle/$name.stdout"
69
+    case $name in
70
+        no_module)          echo "shellfu: cannot find module: $name" ;;
71
+        empty_file)         echo "shellfu: missing version declaration: test/shellfu/$name.sh" ;;
72
+        no_declaration)     echo "shellfu: missing version declaration: test/shellfu/$name.sh" ;;
73
+        deep4)              echo "shellfu: missing version declaration: test/shellfu/$name.sh" ;;
74
+        version_empty)      echo "shellfu: missing version declaration: test/shellfu/$name.sh" ;;
75
+    esac > "oracle/$name.stderr"
76
+}
77
+
78
+mkes() {
79
+    case $name in
80
+        deep1|deep2|deep3|version_spaces) echo 0 ;;
81
+        no_module)                        echo 3 ;;
82
+        *)                                echo 2 ;;
83
+    esac
84
+}
85
+
86
+run_wrapper() {
87
+    #
88
+    # Run shellfu in wrapper
89
+    #
90
+    mkwrapper "$name" | bash
91
+}
92
+
93
+tf_do_subtest() {
94
+    #
95
+    # Prepare, run and check results of test $1
96
+    #
97
+    local name="$1"
98
+    local o_es
99
+    mkdir -p test/shellfu result oracle
100
+    mkmodule
101
+    mkoracle
102
+    o_es=$(mkes)
103
+    tf_testflt -n "$name" -O "oracle/$name.stdout" -E "oracle/$name.stderr" -S "$o_es" run_wrapper
104
+}
105
+
106
+tf_do_subtests