import-fqns added
parent
1a605a7648
commit
778571cd06
32
src/ns.sh
32
src/ns.sh
|
@ -23,6 +23,38 @@
|
|||
__PKGSH_INC_NS=1
|
||||
|
||||
|
||||
##
|
||||
# Import a package as a fully-qualified namespace
|
||||
#
|
||||
# N.B.: This should be used instead of `source` to import a package to
|
||||
# ensure that any additional needed processing is performed; otherwise,
|
||||
# while it will still run, it may not work as expected with pkgsh.
|
||||
#
|
||||
# The package will be sourced and all functions may be accessed by using the
|
||||
# fully-qualified name.
|
||||
#
|
||||
# For example, given `import-fqns foo::bar`, you would access function `baz`
|
||||
# in that namespace as the full `foo::bar::baz`.
|
||||
#
|
||||
# Packages imported using this method will be available to all sourced
|
||||
# files in the current shell (and for subshells if there are any exports);
|
||||
# this is intended to be used internally for package loading prior to
|
||||
# additional processing; it is analgous to `source`.
|
||||
#
|
||||
import-fqns()
|
||||
{
|
||||
local -r ns="$1"
|
||||
local -r pkgpath="${ns//:://}.sh"
|
||||
|
||||
test -f "$pkgpath" -a -r "$pkgpath" || {
|
||||
echo "error: \`$ns' is not a package" >&2
|
||||
return 1
|
||||
}
|
||||
|
||||
source "$pkgpath"
|
||||
}
|
||||
|
||||
|
||||
##
|
||||
# Apply to current namespace
|
||||
#
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
#!/bin/bash
|
||||
# Stub package for testing
|
||||
#
|
||||
# Copyright (C) 2014 Mike Gerwitz
|
||||
#
|
||||
# This file is part of pkgsh.
|
||||
#
|
||||
# pkgsh 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 3 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, see <http://www.gnu.org/licenses/>.
|
||||
##
|
||||
|
||||
|
||||
test::fixture::pkg-stub::try()
|
||||
{
|
||||
echo "pkg-stub $@"
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
#!/bin/bash
|
||||
# Tests importing a fully qualified namespace
|
||||
#
|
||||
# Copyright (C) 2014 Mike Gerwitz
|
||||
#
|
||||
# This file is part of pkgsh.
|
||||
#
|
||||
# pkgsh 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 3 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, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
# This is equalent to `source`ing a package, except that it may perform
|
||||
# additional processing that is required to properly load the package.
|
||||
##
|
||||
|
||||
source src/ns.sh
|
||||
|
||||
|
||||
test-ok()
|
||||
{
|
||||
# this should auto-load test/fixture/pkg-stub.sh
|
||||
import-fqns test::fixture::pkg-stub
|
||||
|
||||
# we should now have access to the functions as if we sourced it
|
||||
result="$( test::fixture::pkg-stub::try foo )"
|
||||
assert "$result" == "pkg-stub foo"
|
||||
}
|
||||
|
||||
|
||||
test-bad()
|
||||
{
|
||||
local -i ok=0
|
||||
local -r badns=test::does::not::exist
|
||||
|
||||
# this package does not exist; should error
|
||||
result="$( import-fqns "$badns" 2>&1 )" || {
|
||||
# good; this is what we want, but we need to handle this failure, since
|
||||
# -e is set
|
||||
ok=1
|
||||
}
|
||||
|
||||
assert "$ok" -eq 1
|
||||
|
||||
# ensure that an error is output
|
||||
[[ "$result" =~ error ]] || assert -z "missing 'error'"
|
||||
[[ "$result" =~ "$badns" ]] || assert -z "missing ns name in error"
|
||||
}
|
||||
|
||||
|
||||
test-ok
|
||||
test-bad
|
||||
|
Loading…
Reference in New Issue