File: install-linux_test_ensure_command.sh

package info (click to toggle)
opkssh 0.8.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,428 kB
  • sloc: sh: 1,981; makefile: 4
file content (125 lines) | stat: -rw-r--r-- 4,365 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/bash
export SHUNIT_RUNNING=1


# Source install-linux.sh
# shellcheck disable=SC1091
source "$(dirname "${BASH_SOURCE[0]}")/../install-linux.sh"

#
# Override functions for mocking
#
command() {
    if [[ "$1" == "-v" && "$2" == "$mock_command_name" ]]; then
        if [[ "$mock_command_exists" == "true" ]]; then
            echo "/usr/bin/$2"
            return 0
        else
            return 1
        fi
    fi
    builtin command "$@"  # fall back to real command
}


# Running tests

test_ensure_command_exists() {
    mock_command_name="curl"
    mock_command_exists=true
    output=$(ensure_command "curl" "curl" "debian" 2>&1)
    result=$?
    assertEquals "Expected ensure_command_exists to return 0 when command exists" 0 $result
    assertEquals "Expected ensure_command_exists to not output anything when command exists" "" "$output"
}

test_ensure_command_missing_using_variables() {
    mock_command_name="foobar"
    mock_command_exists=false
    # shellcheck disable=2034  # used in ensure_command
    OS_TYPE=suse
    output=$(ensure_command "foobar" 2>&1)
    result=$?
    assertEquals "Expected ensure_command_exists to return 1 when command is missing" 1 $result
    assertContains "Expected ensure_command_exists to prompt to install on suse" "$output" "sudo zypper install foobar"
}

test_ensure_command_missing_debian() {
    mock_command_name="curl"
    mock_command_exists=false
    output=$(ensure_command "curl" "curl" "debian" 2>&1)
    result=$?
    assertEquals "Expected ensure_command_exists to return 1 when command is missing" 1 $result
    assertContains "Expected ensure_command_exists to prompt to install on debian" "$output" "sudo apt install curl"
}

test_ensure_command_missing_redhat_with_dnf() {
    mock_command_name="curl"
    mock_command_exists=false
    # Also mock dnf existence
    # shellcheck disable=2317
    command() {
        if [[ "$1" == "-v" && "$2" == "dnf" ]]; then
            return 0  # dnf exists
        fi
        if [[ "$1" == "-v" && "$2" == "$mock_command_name" ]]; then
            return 1  # command is missing
        fi
        builtin command "$@"
    }
    output=$(ensure_command "curl" "curl" "redhat" 2>&1)
    result=$?
    assertEquals "Expected ensure_command_exists to return 1 when command is missing" 1 $result
    assertContains "Expected ensure_command_exists to suggest dnf for redhat if available" "$output" "sudo dnf install curl"
}


test_ensure_command_missing_redhat_without_dnf() {
    mock_command_name="curl"
    mock_command_exists=false
    # Also mock dnf existence
    # shellcheck disable=2317
    command() {
        if [[ "$1" == "-v" && "$2" == "dnf" ]]; then
            return 1  # dnf is missing
        fi
        if [[ "$1" == "-v" && "$2" == "$mock_command_name" ]]; then
            return 1  # command is missing
        fi
        builtin command "$@"
    }
    output=$(ensure_command "curl" "curl" "redhat" 2>&1)
    result=$?
    assertEquals "Expected ensure_command_exists to return 1 when command is missing" 1 $result
    assertContains "Expected ensure_command_exists to suggest dnf for redhat if available" "$output" "sudo yum install curl"
}

test_ensure_command_missing_arch() {
    mock_command_name="curl"
    mock_command_exists=false
    output=$(ensure_command "curl" "curl" "arch" 2>&1)
    result=$?
    assertEquals "Expected ensure_command_exists to return 1 when command is missing" 1 $result
    assertContains "Expected ensure_command_exists to suggest pacman for arch" "$output" "sudo pacman -S curl"
}

test_ensure_command_missing_suse() {
    mock_command_name="curl"
    mock_command_exists=false
    output=$(ensure_command "curl" "curl" "suse" 2>&1)
    result=$?
    assertEquals "Expected ensure_command_exists to return 1 when command is missing" 1 $result
    assertContains "Expected ensure_command_exists to suggest zypper for suse" "$output" "sudo zypper install curl"
}

test_ensure_command_unsupported_os() {
    mock_command_name="curl"
    mock_command_exists=true
    output=$(ensure_command "curl" "curl" "foobar" 2>&1)
    result=$?
    assertEquals "Expected ensure_command_exists to return 1 when it is an Unsupported OS" 1 $result
    assertContains "Expected ensure_command_exists to warn about unsupported OS" "$output" "Unsupported OS type."
}

# shellcheck disable=SC1091
source shunit2