summaryrefslogtreecommitdiffstats
path: root/bin/dtf-gen
blob: 62deea06fb4518cec679fb3e3e94bcafe2f25383 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#! /bin/sh

# Simple testsuite generator.
# Copyright (C) 2015 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# 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.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


## Some important facts about this code.
##
## - it's enough to do 'false' within test and it is failure.
## - each subdirectory file is self-standing
## - each test might have several tags -- future dependencies

DTF_OUTPUT=DTF_OUTPUT
: ${DG=dg}

export opt_distro=fedora-rawhide-x86_64

# config
: ${outputdir=$PWD/$DTF_OUTPUT}
: ${toporigdir=$PWD}
: ${libdir=/usr/lib}
: ${datadir=/usr/share}

: ${dtfdatadir=$datadir/dtf}
: ${dtflibdir=$dtfdatadir/lib}
: ${dtftpldir=$dtfdatadir/tpl}

. "$dtflibdir/default" || exit 1


info ()
{
    echo " * $*"
}


die ()
{
    echo >&2 "FATAL: $*"
    exit 1
}


dg_call ()
{
    "$DG" --distro="$opt_distro".yaml "$@" \
        || die "dg failure"
}


generate_wrapper ()
{
    _run_cmd=$outputdir/$1/run
    # TODO: This is not portable.

    fix_testname=$1
    test "$fix_testname" = '.' && fix_testname=ROOT
    fix_testname=${fix_testname##./}

    echo '#! /bin/sh

# DTF'\''s '\''run'\'' wrapper.
# Copyright (C) 2015 Red Hat, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# 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.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

: ${__DTF_TOP_TEST=:}

abs_dirname ()
{
    abs_dirname_result=`cd "$1" && pwd`
}
abs_dirname "`dirname $0`"
srcdir=$abs_dirname_result

' > "$_run_cmd"

    cat >> "$_run_cmd" <<EOF
export top_srcdir=\$srcdir/$(printf %q "$2")
export testname=$(printf %q "$fix_testname")
EOF

    dg_call --template "$dtftpldir/run.tpl" >> "$_run_cmd"

    chmod +x "$_run_cmd"
}


# count_recursive_walk SUBDIR SRCDIR TOP_SRCDIR
# ---------------------------------------------
count_recursive_walk ()
{
    cd "$1"

    info "directory: $2"

    mkdir -p "$outputdir/$2"

    generate_wrapper "$2" "$3"

    if test -f testcase; then
        dg_call --template testcase --output "$outputdir/$2/testcase"
    fi

    test -f library && dg_call --template library >> "$outputdir/library"

    for _rw_i in *
    do
      __dtf_is_testdir "$_rw_i" || continue
      _rw_fulldir="$2/$_rw_i"

      mkdir -p "$outputdir/$_rw_fulldir" \
          || die "can't create $outputdir/$_rw_fulldir"

      count_recursive_walk "$_rw_i" "$2/$_rw_i" "$3/.."
    done
    cd ..
}


# TODO: option parsing
if test x"$1" == x--distro
then
    test -n "$2" && opt_distro="$2"
fi

case $outputdir in
    /*) : ;;
    *) die "not absolute \$outputdir=$outputdir" ;;
esac

test -d "$outputdir" && {
    chmod u+w "$outputdir" -R
    /bin/rm -rf "$outputdir"
}

mkdir -p "$outputdir"
cat "$dtflibdir/default" > "$outputdir/library"
dg_call --template "$dtflibdir/tests" >> "$outputdir/library"

count_recursive_walk "$toporigdir" . .

# Protect sources!
chmod a-w "$outputdir" -R