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

Switching to XDG standard for file management #7

Merged
merged 1 commit into from
Jan 27, 2025
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
18 changes: 10 additions & 8 deletions install.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#!/bin/bash

read -p "Enter the installation directory [default: $HOME/.shunpo]: " user_input
INSTALL_DIR=${user_input:-"$HOME/.shunpo"}
DEFAULT_INSTALL_DIR=${XDG_DATA_HOME:-$HOME/.local/share}/shunpo
read -p "Enter the installation directory [default: $DEFAULT_INSTALL_DIR]: " user_input
INSTALL_DIR=${user_input:-"$DEFAULT_INSTALL_DIR"}
BASHRC="$HOME/.bashrc"
SHUNPORC="$HOME/.shunporc"
# file containing command definitions:
SHUNPO_CMD="$INSTALL_DIR/shunpo_cmd"

setup() {
mkdir -p $INSTALL_DIR
if [ -f $SHUNPORC ]; then
rm $SHUNPORC
if [ -f $SHUNPO_CMD ]; then
rm $SHUNPO_CMD
fi
touch $SHUNPORC
touch $SHUNPO_CMD
}

add_commands() {
Expand All @@ -27,7 +29,7 @@ add_commands() {
)

for func_definition in "${functions[@]}"; do
echo "$func_definition" >>"$SHUNPORC"
echo "$func_definition" >>"$SHUNPO_CMD"
echo "Created Command: ${func_definition%%()*}"
done
}
Expand All @@ -36,7 +38,7 @@ install() {
cp src/* $INSTALL_DIR

# add sourcing for .shunporc
source_rc_line="source $SHUNPORC"
source_rc_line="source $SHUNPO_CMD"
temp_file=$(mktemp)
sed '/^source.*\.shunporc/d' "$BASHRC" >"$temp_file"
mv "$temp_file" "$BASHRC"
Expand Down
2 changes: 1 addition & 1 deletion src/functions.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

# Default Bookmarks Path.
SHUNPO_BOOKMARKS_FILE="$HOME/.shunpo_bookmarks"
SHUNPO_BOOKMARKS_FILE="$SHUNPO_DIR/.shunpo_bookmarks"

# Function to display bookmarks with pagination.
function shunpo_interact_bookmarks() {
Expand Down
4 changes: 3 additions & 1 deletion tests/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ fi
function setup_env {
HOME=${SHUNPO_TEST_DIR}/home
mkdir -p $HOME
XDG_DATA_HOME=${SHUNPO_TEST_DIR}
mkdir -p $XDG_DATA_HOME
}

function cleanup_env {
Expand Down Expand Up @@ -44,5 +46,5 @@ make_directories() {
}

get_num_bookmarks() {
echo $(wc -l <${SHUNPO_TEST_DIR}/home/.shunpo_bookmarks | tr -d '[:space:]')
echo $(wc -l <${SHUNPO_DIR}/.shunpo_bookmarks | tr -d '[:space:]')
}
37 changes: 18 additions & 19 deletions tests/test_bookmarks.bats
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ setup() {
printf '\n' | ./install.sh
working_dir=$(pwd)
source ${SHUNPO_TEST_DIR}/home/.bashrc
source ${SHUNPO_TEST_DIR}/home/.shunporc
cd ${SHUNPO_TEST_DIR}
}

Expand All @@ -23,17 +22,17 @@ teardown() {
}

@test "Test Install." {
[ -e "${SHUNPO_TEST_DIR}.shunporc" ] && assert_success
[ "$(echo $SHUNPO_DIR)" = "${SHUNPO_TEST_DIR}.shunpo" ] && assert_success
[ -e "${SHUNPO_TEST_DIR}.shunpo/functions.sh" ] && assert_success
[ -e "${SHUNPO_TEST_DIR}.shunpo/colors.sh" ] && assert_success
[ -e "${SHUNPO_TEST_DIR}.shunpo/add_bookmark.sh" ] && assert_success
[ -e "${SHUNPO_TEST_DIR}.shunpo/go_to_bookmark.sh" ] && assert_success
[ -e "${SHUNPO_TEST_DIR}.shunpo/remove_bookmark.sh" ] && assert_success
[ -e "${SHUNPO_TEST_DIR}.shunpo/list_bookmarks.sh" ] && assert_success
[ -e "${SHUNPO_TEST_DIR}.shunpo/clear_bookmarks.sh" ] && assert_success
[ -e "${SHUNPO_TEST_DIR}.shunpo/jump_to_parent.sh" ] && assert_success
[ -e "${SHUNPO_TEST_DIR}.shunpo/jump_to_child.sh" ] && assert_success
[ -e "${SHUNPO_DIR}/shunpo_cmd" ] && assert_success
[ "$(echo $SHUNPO_DIR)" = "${SHUNPO_DIR}" ] && assert_success
[ -e "${SHUNPO_DIR}/functions.sh" ] && assert_success
[ -e "${SHUNPO_DIR}/colors.sh" ] && assert_success
[ -e "${SHUNPO_DIR}/add_bookmark.sh" ] && assert_success
[ -e "${SHUNPO_DIR}/go_to_bookmark.sh" ] && assert_success
[ -e "${SHUNPO_DIR}/remove_bookmark.sh" ] && assert_success
[ -e "${SHUNPO_DIR}/list_bookmarks.sh" ] && assert_success
[ -e "${SHUNPO_DIR}/clear_bookmarks.sh" ] && assert_success
[ -e "${SHUNPO_DIR}/jump_to_parent.sh" ] && assert_success
[ -e "${SHUNPO_DIR}/jump_to_child.sh" ] && assert_success
run declare -F sb && assert_success
run declare -F sg && assert_success
run declare -F sr && assert_success
Expand All @@ -53,7 +52,7 @@ teardown() {
assert_equal "$num_bookmarks" "4"

# Check if bookmark entry is correct.
bookmark2=$(sed -n 3p ${SHUNPO_TEST_DIR}/home/.shunpo_bookmarks)
bookmark2=$(sed -n 3p ${SHUNPO_DIR}/.shunpo_bookmarks)
expected_bookmark2="${SHUNPO_TEST_DIR}/1/2"
assert_equal "$bookmark2" "$expected_bookmark2"
}
Expand All @@ -78,10 +77,10 @@ teardown() {
@test "Test Remove Bookmark." {
# Set up directory structure.
make_directories 1
assert [ -f ${SHUNPO_TEST_DIR}/home/.shunpo_bookmarks ]
assert [ -f ${SHUNPO_DIR}/.shunpo_bookmarks ]

# Store the last bookmark.
bookmark3=$(sed -n 4p ${SHUNPO_TEST_DIR}/home/.shunpo_bookmarks)
bookmark3=$(sed -n 4p ${SHUNPO_DIR}/.shunpo_bookmarks)

# Check failure handling.
run sr -1 >/dev/null && assert_failure
Expand All @@ -98,23 +97,23 @@ teardown() {
assert_equal "$num_bookmarks" "2"

# Check shifting.
bookmark1=$(sed -n 2p ${SHUNPO_TEST_DIR}/home/.shunpo_bookmarks)
bookmark1=$(sed -n 2p ${SHUNPO_DIR}/.shunpo_bookmarks)
assert_equal "$bookmark3" "$bookmark1"

# Remove until file is removed.
run sr 0 && assert_success
run sr 0 && assert_success
refute [ -f ${SHUNPO_TEST_DIR}/home/.shunpo_bookmarks ]
refute [ -f ${SHUNPO_DIR}/.shunpo_bookmarks ]
}

@test "Test Clear Bookmarks." {
# Set up directory structure.
make_directories 1

# Check that the file exists
assert [ -f ${SHUNPO_TEST_DIR}/home/.shunpo_bookmarks ]
assert [ -f ${SHUNPO_DIR}/.shunpo_bookmarks ]

# Confirm that the file is removed after clearing.
run sc && assert_success
refute [ -f ${SHUNPO_TEST_DIR}/home/.shunpo_bookmarks ]
refute [ -f ${SHUNPO_DIR}/.shunpo_bookmarks ]
}
3 changes: 1 addition & 2 deletions tests/test_navigation.bats
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ setup() {
printf '\n' | ./install.sh
working_dir=$(pwd)
source ${SHUNPO_TEST_DIR}/home/.bashrc
source ${SHUNPO_TEST_DIR}/home/.shunporc
source ${SHUNPO_TEST_DIR}/home/.shunpo/functions.sh
source ${SHUNPO_DIR}/functions.sh
cd ${SHUNPO_TEST_DIR}
}

Expand Down
16 changes: 9 additions & 7 deletions uninstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ source $BASHRC

uninstall() {
echo "Uninstalling..."
if [ -f "$HOME/.shunporc" ]; then
rm $HOME/.shunporc
echo "Removed $HOME/.shunporc"
SHUNPO_CMD="$SHUNPO_DIR/shunpo_cmd"
if [ -f "$SHUNPO_CMD" ]; then
rm "$SHUNPO_CMD"
echo "Removed $SHUNPO_CMD"
fi

if [ -f "$HOME/.shunpo_bookmarks" ]; then
rm $HOME/.shunpo_bookmarks
echo "Removed $HOME/.shunpo_bookmarks"
SHUNPO_BOOKMARKS_FILE="$SHUNPO_DIR/.shunpo_bookmarks"
if [ -f $SHUNPO_BOOKMARKS_FILE ]; then
rm $SHUNPO_BOOKMARKS_FILE
echo "Removed $SHUNPO_BOOKMARKS_FILE"
fi

if [ -z "$SHUNPO_DIR" ]; then
Expand All @@ -36,7 +38,7 @@ uninstall() {
fi

temp_file=$(mktemp)
sed '/^source.*\.shunporc/d' "$BASHRC" >"$temp_file"
sed '/^source.*\shunpo_cmd/d' "$BASHRC" >"$temp_file"
mv "$temp_file" "$BASHRC"

temp_file=$(mktemp)
Expand Down
Loading