Skip to content
Open
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
43 changes: 38 additions & 5 deletions aliasme.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,43 @@ _remove() {
}

_excute() {
local alias_name="$1"
if [ -s "$ALIASME_CMD" ];then
while read -u9 -r line; do
if [ "$1" = "$line" ]; then
read -u9 -r line
eval "$line"
if [ "$alias_name" = "$line" ]; then
read -u9 -r cmd
shift # Remove alias name, $@ now has arguments

local final_cmd="$cmd"
local has_placeholder=false

# Handle ?1, ?2, etc. (Positional)
local i=1
for arg in "$@"; do
if [[ "$final_cmd" == *"?"$i* ]]; then
final_cmd="${final_cmd//\?$i/$arg}"
has_placeholder=true
fi
((i++))
done

# Handle ? (All arguments)
if [[ "$final_cmd" == *"?"* ]]; then
# Double check it's not a positional one we missed (e.g. ?99)
local tmp_cmd="${final_cmd//\?[0-9]/}"
if [[ "$tmp_cmd" == *"?"* ]]; then
final_cmd="${final_cmd//\?/"$*"}"
has_placeholder=true
fi
fi

if [ "$has_placeholder" = true ]; then
eval "$final_cmd"
else
# Default: append arguments if any
# shellcheck disable=SC2294
eval "$cmd" "$@"
fi
return 0
fi
done 9< "$ALIASME_CMD"
Expand Down Expand Up @@ -124,14 +156,15 @@ al(){
echo "al add [name] [command] # add alias command with name"
echo "al rm [name] # remove alias by name"
echo "al ls # alias list"
echo "al [name] # execute alias associate with [name]"
echo "al [name] [args] # execute alias associate with [name]"
echo " # use ? for all args, ?1, ?2 for positional args"
echo "al -v # version information"
echo "al -h # help"
elif [ "$1" = "-v" ]; then
echo "aliasme 3.1.0"
echo "visit https://github.com/Jintin/aliasme for more information"
else
if ! _excute "$1" ; then
if ! _excute "$@"; then
echo "not found"
fi
fi
Expand Down
110 changes: 81 additions & 29 deletions test/aliastest.sh
Original file line number Diff line number Diff line change
@@ -1,45 +1,97 @@
#!/bin/bash

. test/assert.sh
# Mock ALIASME_DIR for testing to avoid touching user's real aliases
ALIASME_DIR="/tmp/aliasme_test_$(date +%s)"
export ALIASME_DIR
ALIASME_CMD="$ALIASME_DIR/cmd"
export ALIASME_CMD
mkdir -p "$ALIASME_DIR"

. aliasme.sh

testInit() {
if [[ ! -f ~/.aliasme/path ]]; then
mkdir -p ~/.aliasme && touch ~/.aliasme/path
testAdd() {
local name="$1"
local cmd="$2"
_add "$name" "$cmd" > /dev/null
if [[ $(_list) = *"$name : $cmd"* ]]; then
log_success "Add $name success"
else
log_failure "Add $name failure"
exit 1
fi
}

testAlias() {

name1=testaaa
cmd1=cmdaaa
testAdd "$name1" "$cmd1"

name2=testbbb
cmd2=cmdbbb
testAdd "$name2" "$cmd2"

testRemove "$name1"
testRemove "$name2"
}

testAdd() {
_add "$1" "$2"
if [[ $(_list) = *"$1 : $2"* ]]; then
log_success "path test success"
testRemove() {
local name="$1"
_remove "$name" > /dev/null
if [[ $(_list) = *"$name"* ]]; then
log_failure "Remove $name failure"
exit 1
else
log_failure "path test failure"
log_success "Remove $name success"
fi
}

testRemove() {
_remove "$1"
if [[ $(_list) = *"$1"* ]]; then
log_failure "remove test failure"
testExecute() {
local name="$1"
shift
local expected
expected="${*:$#:1}" # last argument is expected output
# All arguments except the last one are passed to 'al'
local args=()
local i=1
for arg in "$@"; do
if [ $i -lt "$#" ]; then
args+=("$arg")
fi
((i++))
done

# Capture output of execution
local actual
actual=$(al "$name" "${args[@]}")

if [ "$actual" == "$expected" ]; then
log_success "Execute $name with args [${args[*]}] -> $actual"
else
log_success "remove test success"
log_failure "Execute $name: expected [$expected], but got [$actual]"
exit 1
fi
}

testInit
testAlias
# Cleanup function
cleanup() {
rm -rf "$ALIASME_DIR"
}
trap cleanup EXIT

log_header "Basic Operations"
testAdd "hello" "echo hello"
testAdd "world" "echo world"
testRemove "hello"
testRemove "world"

log_header "Dynamic Arguments (Append)"
testAdd "say" "echo"
testExecute "say" "hello" "hello"
testExecute "say" "hello world" "hello world"

log_header "Dynamic Arguments (Fill ?)"
testAdd "greet" "echo hello ?"
testExecute "greet" "Alice" "hello Alice"
testExecute "greet" "Alice and Bob" "hello Alice and Bob"

log_header "Dynamic Arguments (Multi-Fill ?1 ?2)"
testAdd "swap" "echo ?2 ?1"
testExecute "swap" "A" "B" "B A"
testExecute "swap" "first" "second" "second first"

log_header "Mixed Dynamic Arguments"
testAdd "mix" "echo ?2 ? ?1"
testExecute "mix" "one" "two" "three" "two one two three one"
# Note: ? replaces with all arguments "$*", so "one two three"
# ?2 is "two", ?1 is "one"
# Result: "two one two three one"

log_success "All tests passed!"
Loading