From bacffde3e3be1042dd4d4b1947209cf80e7d06fa Mon Sep 17 00:00:00 2001 From: Jakub Surdej Date: Tue, 23 Jan 2024 11:21:36 +0100 Subject: [PATCH] Fix remove.sh script and create its test --- lib/remove.sh | 2 +- tests/clone_test.sh | 0 tests/remove_test.sh | 48 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) mode change 100644 => 100755 tests/clone_test.sh create mode 100755 tests/remove_test.sh diff --git a/lib/remove.sh b/lib/remove.sh index 1d1d977..05279bf 100755 --- a/lib/remove.sh +++ b/lib/remove.sh @@ -1,6 +1,6 @@ #!/bin/bash -set -euo pipefail +set -eo pipefail source "${BASH_SOURCE%/*}/shared/utils.sh" diff --git a/tests/clone_test.sh b/tests/clone_test.sh old mode 100644 new mode 100755 diff --git a/tests/remove_test.sh b/tests/remove_test.sh new file mode 100755 index 0000000..aeee115 --- /dev/null +++ b/tests/remove_test.sh @@ -0,0 +1,48 @@ +#!/usr/bin/env bats + +LIB_DIR="./lib" +TEST_MGC_BASE_DIR="./tmp-test-remove" +PROFILE_DIR="$TEST_MGC_BASE_DIR/.mgc/profiles" + +setup() { + MGC_DIR="$TEST_MGC_BASE_DIR/.mgc" + export MGC_DIR + + mkdir -p "$PROFILE_DIR" +} + +teardown() { + rm -rf "$TEST_MGC_BASE_DIR" +} + +create_test_profile() { + local profile_name=$1 + mkdir -p "$PROFILE_DIR/$profile_name" + echo "/path/to/test/id_rsa" > "$PROFILE_DIR/$profile_name/ssh_key" + echo "test@example.com" > "$PROFILE_DIR/$profile_name/email" + echo "testuser" > "$PROFILE_DIR/$profile_name/username" +} + +simulate_remove_profile() { + local profile_name=$1 + local confirmation=$2 + echo $confirmation | bash "$LIB_DIR/remove.sh" "$profile_name" +} + +@test "Remove existing profile" { + create_test_profile "testProfile" + simulate_remove_profile "testProfile" "y" + [ ! -d "$PROFILE_DIR/testProfile" ] +} + +@test "Fail to remove non-existent profile" { + run simulate_remove_profile "nonExistentProfile" "y" + [ "$status" -eq 1 ] + [[ "$output" == *"Error: Profile 'nonExistentProfile' does not exist."* ]] +} + +@test "Cancel profile removal" { + create_test_profile "testProfile" + simulate_remove_profile "testProfile" "n" + [ -d "$PROFILE_DIR/testProfile" ] +}