Skip to content

Commit bf2597a

Browse files
committed
Read configuration from debian/tests/autopkgtest-pkg-${type}.conf
For Python packages, specifying the module name to be imported via debian/tests/pkg-python/import-name is now deprecated and will generate a warning. Gbp-DCh: full
1 parent acd485f commit bf2597a

File tree

9 files changed

+158
-10
lines changed

9 files changed

+158
-10
lines changed

Makefile

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
PREFIX ?= /usr/local
2-
install_support = $(filter-out install-common,$(patsubst support/%, install-%, $(wildcard support/*)))
2+
install_support = $(filter-out install-common install-autodep8lib.sh,$(patsubst support/%, install-%, $(wildcard support/*)))
33

44
all: autodep8.1
55

66
autodep8.1: autodep8.pod
77
pod2man --verbose --name autodep8 -c '' -r '' --utf8 $< $@ || ($(RM) $@; false)
88

99
autodep8.pod: README.md examples.md
10-
(sed -e '/examples.md/ r examples.md' README.md | sed -e '/examples.md/d; s/^##/=head2/; s/^#/=head1/; s/\*\*\([^\*]*\)\*\*/B<\1>/g; s/\*\([^\*]*\)\*/I<\1>/g; ' > $@) || ($(RM) $@; false)
10+
(sed -e '/examples.md/ r examples.md' README.md | sed -e '/examples.md/d; /```/,/```/ s/^/ /; /```/d; s/^##/=head2/; s/^#/=head1/; s/\*\*\([^\*]*\)\*\*/B<\1>/g; s/\*\([^\*]*\)\*/I<\1>/g' > $@) || ($(RM) $@; false)
1111

1212
update-examples:
1313
sh examples.sh > examples.md
1414

1515
install: $(install_support)
1616
install -m 644 support/common $(DESTDIR)/$(PREFIX)/share/autodep8/support
17+
install -m 644 support/autodep8lib.sh $(DESTDIR)/$(PREFIX)/share/autodep8/support
1718
install -d $(DESTDIR)/$(PREFIX)/bin
1819
install -m 755 autodep8 $(DESTDIR)/$(PREFIX)/bin
1920
install -d $(DESTDIR)/$(PREFIX)/share/man/man1

README.md

+31-6
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,39 @@ each supported package type is tried against a set of heuristics, based on
3737
packages names, build dependencies. specific files under debian/, or a
3838
combination of those.
3939

40-
# NOTES ABOUT SOME PACKAGE TYPES
40+
# PACKAGE-SPECIFIC CONFIGURATION
4141

42-
## PYTHON PACKAGES
42+
Packages can provide configuration for **autodep8** in
43+
*debian/tests/autopkgtest-pkg-${PACKAGETYPE}.conf*. The file format is the the
44+
following:
4345

44-
If the module name to be imported cannot be inferred from the name of the
45-
Debian package, put it in `debian/tests/pkg-python/import-name`. For
46-
example, `python3-xlib` is used via `import Xlib`, so
47-
`echo Xlib > debian/tests/pkg-python/import-name` would be appropriate.
46+
```
47+
# comment lines start with # and are ignored.
48+
# note that #'s only mark comments when in the beginning of the line
49+
# empty lines are also ignored
50+
51+
# values are set in this format:
52+
var1=value1
53+
54+
# spaces around the = sign are allowed
55+
var2 = value 2
56+
57+
# backslashes allow one to set values that span multiple lines.
58+
# Note that the newline is removed in the final value, though
59+
# The following is equivalent to "multiline=value1, value2"
60+
variable = value1, \
61+
value2
62+
```
63+
64+
The following configuration variables are supported:
65+
66+
## python (debian/tests/autopkgtest-pkg-python.conf)
67+
68+
**import_name**: name of the module to import, if it cannot be inferred from
69+
the name of the Debian package. For example, `python3-xlib` is used via `import
70+
Xlib`, so `import_name = Xlib` would be appropriate. This used to be
71+
configured by writing to `debian/tests/pkg-python/import-name`, but that is now
72+
deprecated.
4873

4974
# COMBINING AUTO-GENERATED TESTS WITH MANUALLY SPECIFIED ONES
5075

autodep8

+3
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,14 @@ case "${program_path}" in
6363
;;
6464
esac
6565

66+
. "${support_dir}"/autodep8lib.sh
67+
6668
for packagedir in $(find $support_dir -mindepth 1 -type d | LC_ALL=C.UTF-8 sort); do
6769
packagetype=$(basename $packagedir)
6870
if detect_by_control_field $packagetype ||
6971
detect_by_source_contents "$packagedir"
7072
then
73+
read_config $packagetype
7174
$support_dir/$packagetype/generate
7275
exit 0
7376
fi

support/autodep8lib.sh

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
read_config() {
2+
packagetype="${1}"
3+
config="debian/tests/autopkgtest-pkg-${packagetype}.conf"
4+
[ ! -f "${config}" ] && return
5+
6+
tmpfile="$(mktemp)"
7+
sed -e '
8+
/^\s*#/d; # remove comment lines
9+
/^\s*$/d; # remove empty lines
10+
{ :a /\\$/N; s/\\\n//; ta } # fold lines ending with backslash into next
11+
' "${config}" > "${tmpfile}"
12+
while read line; do
13+
conf="$(echo "$line" | sed -e 's/\s*=\s*/=/')" # remove spaces around first =
14+
if echo "${conf}" | grep -q '^[a-zA-Z_][a-zA-Z_0-9]*='; then
15+
export "pkg_${packagetype}_${conf}"
16+
echo "$conf"
17+
else
18+
echo "W: ${config}: invalid configuration line: ${line} (ignored)" >&2
19+
fi
20+
done < "${tmpfile}"
21+
rm -f "${tmpfile}"
22+
}

support/python/generate

+4-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ fi
6060

6161
module=$(echo "$module" | sed 's/-/_/g')
6262

63-
if [ -e debian/tests/pkg-python/import-name ]; then
63+
if [ -n "${pkg_python_import_name:-}" ]; then
64+
module="${pkg_python_import_name}"
65+
elif [ -e debian/tests/pkg-python/import-name ]; then
66+
echo "W: using debian/tests/pkg-python/import-name is deprecated; please set \`import_name\` in debian/tests/autopkgtest-pkg-python.conf instead." >&2
6467
module=$(perl -ne 'next if /^\s*(#|$)/; print; last;' debian/tests/pkg-python/import-name)
6568
fi
6669

test/autodep8lib_test.sh

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
. $(dirname $0)/helper.sh
2+
. "${AUTODEP8_SUPPORT_DIR}/autodep8lib.sh"
3+
4+
run_read_config() {
5+
has debian/tests/autopkgtest-pkg-ruby.conf "$@"
6+
check_run read_config ruby
7+
}
8+
9+
assertInvalidConfig() {
10+
assertEquals "W: debian/tests/autopkgtest-pkg-ruby.conf: invalid configuration line: ${1} (ignored)" "$(cat stderr)"
11+
}
12+
13+
test_no_config() {
14+
check_run read_config ruby
15+
assertEquals "" "$(cat stdout)"
16+
assertEquals "" "$(cat stderr)"
17+
}
18+
19+
test_read_config_valid() {
20+
run_read_config 'foo=bar'
21+
assertEquals "foo=bar" "$(cat stdout)"
22+
}
23+
24+
test_read_config_invalid_single_word() {
25+
run_read_config 'foo'
26+
assertEquals "" "$(cat stdout)"
27+
assertInvalidConfig foo
28+
}
29+
30+
test_read_config_invalid_no_assignment() {
31+
run_read_config 'foo bar'
32+
assertEquals "" "$(cat stdout)"
33+
assertInvalidConfig 'foo bar'
34+
}
35+
36+
test_read_config_invalid_variable() {
37+
run_read_config 'foo bar=baz'
38+
assertEquals "" "$(cat stdout)"
39+
assertInvalidConfig 'foo bar=baz'
40+
}
41+
42+
test_read_config_invalid_variable_an_whitespace() {
43+
run_read_config 'foo bar = baz'
44+
assertEquals "" "$(cat stdout)"
45+
assertInvalidConfig 'foo bar = baz'
46+
}
47+
48+
test_read_config_invalid_command_injection() {
49+
run_read_config '/bin/sh -c "poweroff"'
50+
assertEquals "" "$(cat stdout)"
51+
assertInvalidConfig '/bin/sh -c "poweroff"'
52+
}
53+
54+
test_read_config_remove_comments() {
55+
run_read_config '# comment\nfoo=bar\n# more comments'
56+
assertEquals "foo=bar" "$(cat stdout)"
57+
assertEquals "" "$(cat stderr)"
58+
}
59+
60+
test_read_config_multiline() {
61+
run_read_config 'foo=bar \\\nbaz'
62+
assertEquals "foo=bar baz" "$(cat stdout)"
63+
}
64+
65+
test_read_config_remove_whitespace_around_equals() {
66+
run_read_config 'foo = bar'
67+
assertEquals 'foo=bar' "$(cat stdout)"
68+
}
69+
70+
test_read_config_remove_whitespace_multiple_equals() {
71+
run_read_config 'foo = bar = baz'
72+
assertEquals 'foo=bar = baz' "$(cat stdout)"
73+
}
74+
75+
test_read_config_exports_variables() {
76+
run_read_config 'foo=bar'
77+
assertEquals "bar" "${pkg_ruby_foo}"
78+
}
79+
80+
. shunit2

test/doc_test.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
test_updated_examples() {
2-
for pkgtype in $(ls --ignore=common -1 support/); do
2+
for pkgtype in $(ls --ignore=common --ignore=autodep8lib.sh -1 support/); do
33
assertTrue "missing example for $pkgtype in examples.md; update examples.in and run \`make update-examples\`" "grep '^## $pkgtype' examples.md"
44
done
55
}

test/helper.sh

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
if [ -z "$AUTOPKGTEST_TMP" ]; then
22
# only use local binaries if not testing the instaled package
33
export PATH="$(readlink -f $(dirname $0))/..:$PATH"
4+
export AUTODEP8_SUPPORT_DIR=$(dirname $0)/../support
5+
else
6+
export AUTODEP8_SUPPORT_DIR="/usr/share/autodep8/support"
47
fi
58

69
run() {

test/python_test.sh

+11
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ test_python_unusual_name_py3() {
2424
assertFalse 'not using wrong name' 'grep --quiet "import foo;" stdout'
2525
assertFalse 'dont have py2 test' 'grep --quiet "pyversions" stdout'
2626
assertTrue 'have py3 test' 'grep --quiet "py3versions" stdout'
27+
assertTrue 'grep "W:.*deprecated" stderr'
28+
}
29+
30+
test_python_unusual_name_py3_via_config() {
31+
has 'debian/control' 'Source: python-foo\n\nPackage:python3-foo'
32+
has 'debian/tests/autopkgtest-pkg-python.conf' 'import_name = Foo'
33+
check_run autodep8
34+
assertTrue 'get upstream name' 'grep --quiet "import Foo;" stdout'
35+
assertFalse 'not using wrong name' 'grep --quiet "import foo;" stdout'
36+
assertFalse 'dont have py2 test' 'grep --quiet "pyversions" stdout'
37+
assertTrue 'have py3 test' 'grep --quiet "py3versions" stdout'
2738
}
2839

2940
test_python_underscore_py3() {

0 commit comments

Comments
 (0)