Add tests for parse_url and follow Library export best practices

This commit is contained in:
Kevin van Zonneveld 2016-02-17 12:58:18 +01:00
parent 180282d97d
commit c3f5bcf8c9
3 changed files with 55 additions and 2 deletions

View File

@ -52,6 +52,7 @@ Although *3* introduces a node.js dependency, this does allow for easy version p
### master (Unreleased)
- Add tests for `ini_val` and follow Library export best practices
- Add tests for `parse_url` and follow Library export best practices
### v1.2.1 (2016-02-17)

View File

@ -1,8 +1,35 @@
#!/usr/bin/env bash
# BASH3 Boilerplate: parse_url
#
# This file:
# - Takes a URL and parses protocol, user, pass, host, port, path.
#
# More info:
# - https://github.com/kvz/bash3boilerplate
#
# Based on:
# - http://stackoverflow.com/a/6174447/151666
#
# Version: 1.2.1
#
# Authors:
# - Kevin van Zonneveld (http://kvz.io)
#
# Usage as a function:
#
# source parse_url.sh
# parse_url 'http://johndoe:abc123@example.com:8080/index.html' pass
#
# Usage as a command:
#
# parse_url.sh 'http://johndoe:abc123@example.com:8080/index.html'
#
# Licensed under MIT
# Copyright (c) 2016 Kevin van Zonneveld (http://kvz.io)
function parse_url() {
# Based on http://stackoverflow.com/a/6174447/151666
local parse="${1}"
local need="${2}"
local need="${2:-}"
local proto="$(echo $parse | grep :// | sed -e's,^\(.*://\).*,\1,g')"
local url="$(echo ${parse/$proto/})"
@ -39,3 +66,10 @@ function parse_url() {
echo ""
fi
}
if [ "${BASH_SOURCE[0]}" != ${0} ]; then
export -f parse_url
else
parse_url "${@}"
exit ${?}
fi

View File

@ -0,0 +1,18 @@
#!/usr/bin/env bash
set -o pipefail
set -o errexit
set -o nounset
# set -o xtrace
# Set magic variables for current FILE & DIR
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
__file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
__base="$(basename ${__file} .sh)"
__root="$(cd "$(dirname $(dirname $(dirname "${__dir}")))" && pwd)"
bash "${__root}/src/parse_url.sh" 'http://johndoe:abc123@example.com:8080/index.html' pass
bash "${__root}/src/parse_url.sh" 'http://johndoe:abc123@example.com:8080/index.html'
source ${__root}/src/parse_url.sh
parse_url 'http://johndoe:abc123@example.com:8080/index.html' pass
parse_url 'http://johndoe:abc123@example.com:8080/index.html'