public
Description: Phusion Passenger (mod_rails)
Homepage: http://www.modrails.com/
Clone URL: git://github.com/FooBarWidget/passenger.git
Click here to lend your support to: passenger and make a donation at www.pledgie.com !
passenger / test / UtilsTest.cpp
100644 112 lines (91 sloc) 2.487 kb
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
#include "tut.h"
#include "Utils.h"
#include <unistd.h>
#include <limits.h>
 
using namespace Passenger;
using namespace std;
 
namespace tut {
  struct UtilsTest {
    vector<string> output;
    string oldPath;
    
    UtilsTest() {
      oldPath = getenv("PATH");
    }
    
    ~UtilsTest() {
      setenv("PATH", oldPath.c_str(), 1);
    }
  };
 
  DEFINE_TEST_GROUP(UtilsTest);
 
  /***** Test split() *****/
 
  TEST_METHOD(1) {
    split("", ':', output);
    ensure_equals(output.size(), 1u);
    ensure_equals(output[0], "");
  }
  
  TEST_METHOD(2) {
    split("hello world", ':', output);
    ensure_equals(output.size(), 1u);
    ensure_equals(output[0], "hello world");
  }
  
  TEST_METHOD(3) {
    split("hello world:foo bar", ':', output);
    ensure_equals(output.size(), 2u);
    ensure_equals(output[0], "hello world");
    ensure_equals(output[1], "foo bar");
  }
  
  TEST_METHOD(4) {
    split("hello world:", ':', output);
    ensure_equals(output.size(), 2u);
    ensure_equals(output[0], "hello world");
    ensure_equals(output[1], "");
  }
  
  TEST_METHOD(5) {
    split(":hello world", ':', output);
    ensure_equals(output.size(), 2u);
    ensure_equals(output[0], "");
    ensure_equals(output[1], "hello world");
  }
  
  TEST_METHOD(6) {
    split("abc:def::ghi", ':', output);
    ensure_equals(output.size(), 4u);
    ensure_equals(output[0], "abc");
    ensure_equals(output[1], "def");
    ensure_equals(output[2], "");
    ensure_equals(output[3], "ghi");
  }
  
  TEST_METHOD(7) {
    split("abc:::def", ':', output);
    ensure_equals(output.size(), 4u);
    ensure_equals(output[0], "abc");
    ensure_equals(output[1], "");
    ensure_equals(output[2], "");
    ensure_equals(output[3], "def");
  }
  
  
  /**** Test findSpawnServer() ****/
  
  TEST_METHOD(8) {
    // If $PATH is empty, it should not find anything.
    setenv("PATH", "", 1);
    ensure_equals(findSpawnServer(), "");
  }
  
  TEST_METHOD(9) {
    // It should ignore relative paths.
    setenv("PATH", "../bin", 1);
    ensure_equals(findSpawnServer(), "");
  }
  
  TEST_METHOD(10) {
    // It should find in $PATH.
    char cwd[PATH_MAX];
    string binpath(getcwd(cwd, sizeof(cwd)));
    binpath.append("/../bin");
    setenv("PATH", binpath.c_str(), 1);
    ensure("Spawn server is found.", !findSpawnServer().empty());
  }
  
  TEST_METHOD(11) {
    // It should use _passengerRoot_.
    ensure_equals(findSpawnServer("/foo"), "/foo/bin/passenger-spawn-server");
  }
  
  TEST_METHOD(12) {
    // It ignore trailing slash in _passengerRoot_.
    ensure_equals(findSpawnServer("/foo/"), "/foo/bin/passenger-spawn-server");
  }
}