|
| 1 | +#!/usr/bin/env bats |
| 2 | + |
| 3 | +setup() { |
| 4 | + # Create a temporary directory for TIPS_FOLDER |
| 5 | + export TIPS_FOLDER="$(mktemp -d)" |
| 6 | + |
| 7 | + # Create sample tips files |
| 8 | + echo -e "Tip 1 in English\nTip 2 in English" >"$TIPS_FOLDER/en.txt" |
| 9 | + echo -e "Consejo 1 en Español\nConsejo 2 en Español" >"$TIPS_FOLDER/es.txt" |
| 10 | + |
| 11 | + # Path to the script being tested |
| 12 | + SCRIPT_PATH="./cli-tips.sh" |
| 13 | +} |
| 14 | + |
| 15 | +teardown() { |
| 16 | + # Remove the temporary TIPS_FOLDER |
| 17 | + rm -rf "$TIPS_FOLDER" |
| 18 | +} |
| 19 | + |
| 20 | +@test "Displays help with -h option" { |
| 21 | + run "$SCRIPT_PATH" -h |
| 22 | + [ "$status" -eq 0 ] |
| 23 | + [[ "$output" == *"Usage:"* ]] |
| 24 | +} |
| 25 | + |
| 26 | +@test "Displays help with --help option" { |
| 27 | + run "$SCRIPT_PATH" --help |
| 28 | + [ "$status" -eq 0 ] |
| 29 | + [[ "$output" == *"Usage:"* ]] |
| 30 | +} |
| 31 | + |
| 32 | +@test "Errors on unknown option" { |
| 33 | + run "$SCRIPT_PATH" --unknown |
| 34 | + [ "$status" -ne 0 ] |
| 35 | + [[ "$output" == *"Unknown option: --unknown"* ]] |
| 36 | +} |
| 37 | + |
| 38 | +@test "Prints a tip in default language" { |
| 39 | + run "$SCRIPT_PATH" |
| 40 | + [ "$status" -eq 0 ] |
| 41 | + [[ "$output" == "Tip "* ]] |
| 42 | +} |
| 43 | + |
| 44 | +@test "Prints a tip in specified language" { |
| 45 | + run "$SCRIPT_PATH" --language es |
| 46 | + [ "$status" -eq 0 ] |
| 47 | + [[ "$output" == "Consejo "* ]] |
| 48 | +} |
| 49 | + |
| 50 | +@test "Filters tips with --about keyword" { |
| 51 | + # Add a tip containing the keyword 'git' to the English tips |
| 52 | + echo -e "Tip about git" >>"$TIPS_FOLDER/en.txt" |
| 53 | + |
| 54 | + run "$SCRIPT_PATH" --about git |
| 55 | + [ "$status" -eq 0 ] |
| 56 | + [[ "$output" == "Tip about git" ]] |
| 57 | +} |
| 58 | + |
| 59 | +@test "Filters tips with --about keyword and --language option" { |
| 60 | + # Add a tip containing the keyword 'git' to the English tips |
| 61 | + echo -e "Consejo sobre git" >>"$TIPS_FOLDER/es.txt" |
| 62 | + |
| 63 | + run "$SCRIPT_PATH" --about git --language es |
| 64 | + [ "$status" -eq 0 ] |
| 65 | + [[ "$output" == "Consejo sobre git" ]] |
| 66 | +} |
| 67 | + |
| 68 | +@test "Exits if no matching tips are found" { |
| 69 | + run "$SCRIPT_PATH" --about nonexistent |
| 70 | + [ "$status" -eq 0 ] |
| 71 | + [ -z "$output" ] |
| 72 | +} |
| 73 | + |
| 74 | +@test "Uses default language if specified language file is missing" { |
| 75 | + rm "$TIPS_FOLDER/es.txt" |
| 76 | + run "$SCRIPT_PATH" --language es |
| 77 | + [ "$status" -eq 0 ] |
| 78 | + [[ "$output" == "Tip "* ]] |
| 79 | +} |
| 80 | + |
| 81 | +@test "Errors if no language is specified after --language option" { |
| 82 | + run "$SCRIPT_PATH" --language |
| 83 | + [ "$status" -ne 0 ] |
| 84 | + [[ "$output" == *"Error: No language specified"* ]] |
| 85 | +} |
| 86 | + |
| 87 | +@test "Handles missing TIPS_FOLDER gracefully" { |
| 88 | + rm -rf "$TIPS_FOLDER" |
| 89 | + run "$SCRIPT_PATH" |
| 90 | + [ "$status" -ne 0 ] |
| 91 | +} |
| 92 | + |
| 93 | +@test "Handles empty tips file gracefully" { |
| 94 | + echo -n "" >"$TIPS_FOLDER/en.txt" |
| 95 | + run "$SCRIPT_PATH" |
| 96 | + [ "$status" -eq 0 ] |
| 97 | + [ -z "$output" ] |
| 98 | +} |
0 commit comments