1#!/usr/bin/perl -w
2#
3# Copyright (C) 2012 Daniel Bates (dbates@intudata.com). All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
8# 1.  Redistributions of source code must retain the above copyright
9#     notice, this list of conditions and the following disclaimer.
10# 2.  Redistributions in binary form must reproduce the above copyright
11#     notice, this list of conditions and the following disclaimer in the
12#     documentation and/or other materials provided with the distribution.
13#
14# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
15# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17# DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR
18# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24
25# Unit tests of VCSUtils::runCommand().
26
27use strict;
28use warnings;
29
30use Test::More;
31use VCSUtils;
32
33use constant ENOENT => 2; # See <errno.h>
34
35# The array of test cases.
36my @testCaseHashRefs = (
37{
38    # New test
39    testName => "Simple",
40    inputArgs => ["echo", "hello"],
41    expectedReturn => {
42        exitStatus => 0,
43        stdout => "hello\n"
44    }
45},
46{
47    # New test
48    testName => "Multiple commands",
49    inputArgs => ["echo", "first-command;echo second-command"],
50    expectedReturn => {
51        exitStatus => 0,
52        stdout => "first-command;echo second-command\n"
53    }
54},
55{
56    # New test
57    testName => "Non-existent command",
58    inputArgs => ["/usr/bin/non-existent-command"],
59    expectedReturn => {
60        exitStatus => ENOENT
61    }
62}
63);
64
65my $testCasesCount = @testCaseHashRefs;
66plan(tests => $testCasesCount); # Total number of assertions.
67
68foreach my $testCase (@testCaseHashRefs) {
69    my $testNameStart = "runCommand(): $testCase->{testName}: comparing";
70
71    my $got = VCSUtils::runCommand(@{$testCase->{inputArgs}});
72    my $expectedReturn = $testCase->{expectedReturn};
73
74    is_deeply($got, $expectedReturn, "$testNameStart return value.");
75}
76