Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quote and exports operation #26

Merged
merged 7 commits into from
Oct 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ env:
matrix:
# Use _ZSH_VERSION since if ZSH_VERSION is present, travis cacher thinks it
# is running in zsh and tries to use zsh specific functions.
# see new versions released at http://zsh.sourceforge.net/News/
- _ZSH_VERSION=5.8
- _ZSH_VERSION=5.7.1
- _ZSH_VERSION=5.6.2
- _ZSH_VERSION=5.5.1
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ Zsh Diractions
*Doing Anything, Anywhere, from here*

[![Tag Version](https://img.shields.io/github/tag/AdrieanKhisbe/diractions.svg)](https://github.com/AdrieanKhisbe/diractions/tags)
[![Build Status](https://img.shields.io/travis/AdrieanKhisbe/diractions?label=travis%20build&logo=travis)](https://travis-ci.org/AdrieanKhisbe/diractions)
[![Build Status](https://img.shields.io/travis/com/AdrieanKhisbe/diractions?label=travis%20build&logo=travis)](https://travis-ci.com/AdrieanKhisbe/diractions)
[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/AdrieanKhisbe/diractions/Diractions%20CI/package-refresh?label=action%20build&logo=github)](https://github.com/AdrieanKhisbe/diractions/actions)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
[![Join the chat https://gitter.im/AdrieanKhisbe/diractions](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/AdrieanKhisbe/diractions)


Expand Down Expand Up @@ -208,6 +209,11 @@ Here are the main ones:
- `DIRACTION_DEF_FILE` : the name of the file containing your diraction definition
- `DIRACTION_EDITOR` : which editor command is used for the edit command
- `DIRACTION_INTERACTIVE_PROMPT` : the "prompt" for the "interactive" command
- `DIRACTION_EXPORT_VARIABLES` :
- whether the `_dir` variables should be exported to the child processes.
- Off by default, can be activated setting variable to `true`
- note: this was the original _(only)_ behavior for versions prior to `v0.18.0`
- `DIRACTION_READONLY_VARIABLES` : whether the `_dir` variables should be made read only. Deactivated by default, activate with `true`

## History

Expand Down
20 changes: 15 additions & 5 deletions diractions.plugin.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ declare -gA _DIRACTION_HELP
-set-default () {
# ¤note: from antigen
local arg_name="$1"
local arg_value="$2"
eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'"
local default_arg_value="$2"
if [ -z "$(eval echo \$$arg_name)" ];then
eval $arg_name='${(q)default_arg_value}'
fi
# §see: make it not exportable?
}
#' function to easily declare help associated to the provided command
Expand All @@ -61,6 +63,8 @@ declare -gA _DIRACTION_HELP
-set-default DIRACTION_DEF_FILE "$HOME/.diractions" # §maybe rename to config file
-set-default DIRACTION_BROWSER # §todo: update
-set-default DIRACTION_AUTO_CONFIG true
-set-default DIRACTION_EXPORT_VARIABLES false
-set-default DIRACTION_READONLY_VARIABLES false # §todo: experiment, and maybe make it default (or maybe not: could not unset then)

# --
if [[ -z $DIRACTION_DISPATCH_WHITELIST ]]; then
Expand Down Expand Up @@ -152,9 +156,15 @@ function diraction-create() {
# create variable if not already bound
if [ -z "${(P)var}" ] ; then
# ¤note: déréférencement de variable: ${(P)var}
export $var="$(eval echo "$dir")"
# §see: keep export?
# §maybe: readonly probably better?? (maybe not: could not unset then)
local value="$(eval echo "$dir")"
if [[ "$DIRACTION_EXPORT_VARIABLES" =~ ^(true|yes|y)$ ]] ; then
export $var="$value"
else
eval "$var=${(q)value}"
fi
if [[ "$DIRACTION_READONLY_VARIABLES" =~ ^(true|yes|y)$ ]] ; then
readonly $var
fi
fi
# create an alias: call to the _diraction-dispach function with directory as argument
alias "$alias"="_diraction-dispatch \"$dir\""
Expand Down
80 changes: 80 additions & 0 deletions test/diraction_integration_shpec.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/env zsh

setopt aliases

plugin="$(dirname $0:A)/../diractions.plugin.zsh"

describe "Integration Diraction"

it "basic script"
output="$(zsh 2>&1 << BASIC_SCRIPT
set -e
DIRACTION_AUTO_CONFIG=false
source $plugin

diraction-batch-create << "DIR"
dir-tmp /tmp
DIR
echo \$_dir_tmp
dir-tmp - pwd
dir-tmp
pwd

zsh << SUB_SHELL
echo dir-tmp:\\\$_dir_tmp:
SUB_SHELL
BASIC_SCRIPT
)"
_status=$?
assert equal $_status 0

assert equal "$output" "/tmp\n/tmp\n/tmp\ndir-tmp::"
end

it "basic script with exports"
output="$(zsh 2>&1 << BASIC_SCRIPT
set -e
DIRACTION_AUTO_CONFIG=false
DIRACTION_EXPORT_VARIABLES=true
source $plugin

diraction-batch-create << "DIR"
dir-tmp /tmp
DIR
echo \$_dir_tmp
dir-tmp - pwd
dir-tmp
pwd

zsh << SUB_SHELL
echo dir-tmp:\\\$_dir_tmp:
SUB_SHELL
BASIC_SCRIPT
)"
_status=$?
assert equal $_status 0

assert equal "$output" "/tmp\n/tmp\n/tmp\ndir-tmp:/tmp:"
end

it "basic script with readonly"
output="$(zsh 2>&1 << BASIC_SCRIPT
set -e
DIRACTION_AUTO_CONFIG=false
DIRACTION_READONLY_VARIABLES=true
source $plugin

diraction-batch-create << "DIR"
ro-dir-tmp /tmp
DIR
echo \$_ro_dir_tmp
readonly _ro_dir_tmp
_ro_dir_tmp="sorry you can't"
BASIC_SCRIPT
)"
_status=$?
assert equal $_status 1

assert equal "$output" "/tmp\nzsh: read-only variable: _ro_dir_tmp"
end
end