MDL-34147 use cygwin style paths in phpunit hints
[moodle.git] / lib / phpunit / bootstraplib.php
CommitLineData
7b0ff213
PS
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * PHPUnit bootstrap function
19 *
20 * Note: these functions must be self contained and must not rely on any library or include
21 *
22 * @package core
23 * @category phpunit
24 * @copyright 2012 Petr Skoda {@link http://skodak.org}
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27
0d8e51a6
PS
28define('PHPUNIT_EXITCODE_PHPUNITMISSING', 129);
29define('PHPUNIT_EXITCODE_PHPUNITWRONG', 130);
30define('PHPUNIT_EXITCODE_PHPUNITEXTMISSING', 131);
31define('PHPUNIT_EXITCODE_CONFIGERROR', 135);
32define('PHPUNIT_EXITCODE_CONFIGWARNING', 136);
33define('PHPUNIT_EXITCODE_INSTALL', 140);
34define('PHPUNIT_EXITCODE_REINSTALL', 141);
35
7b0ff213
PS
36/**
37 * Print error and stop execution
38 * @param int $errorcode The exit error code
39 * @param string $text An error message to display
40 * @return void stops code execution with error code
41 */
42function phpunit_bootstrap_error($errorcode, $text = '') {
43 switch ($errorcode) {
219d1a4e
PS
44 case 0:
45 // this is not an error, just print information and exit
46 break;
7b0ff213
PS
47 case 1:
48 $text = 'Error: '.$text;
49 break;
0d8e51a6
PS
50 case PHPUNIT_EXITCODE_PHPUNITMISSING:
51 $text = "Moodle can not find PHPUnit PEAR library";
52 break;
53 case PHPUNIT_EXITCODE_PHPUNITWRONG:
b5b10866
PS
54 $text = 'Moodle requires PHPUnit 3.6.x, '.$text.' is not compatible';
55 break;
0d8e51a6
PS
56 case PHPUNIT_EXITCODE_PHPUNITEXTMISSING:
57 $text = 'Moodle can not find required PHPUnit extension '.$text;
b5b10866 58 break;
0d8e51a6
PS
59 case PHPUNIT_EXITCODE_CONFIGERROR:
60 $text = "Moodle PHPUnit environment configuration error:\n".$text;
7b0ff213 61 break;
0d8e51a6
PS
62 case PHPUNIT_EXITCODE_CONFIGWARNING:
63 $text = "Moodle PHPUnit environment configuration warning:\n".$text;
7b0ff213 64 break;
0d8e51a6 65 case PHPUNIT_EXITCODE_INSTALL:
5f9ba165
PS
66 $path = phpunit_bootstrap_cli_argument_path('/admin/tool/phpunit/cli/init.php');
67 $text = "Moodle PHPUnit environment is not initialised, please use:\n php $path";
7b0ff213 68 break;
0d8e51a6 69 case PHPUNIT_EXITCODE_REINSTALL:
5f9ba165
PS
70 $path = phpunit_bootstrap_cli_argument_path('/admin/tool/phpunit/cli/init.php');
71 $text = "Moodle PHPUnit environment was initialised for different version, please use:\n php $path";
7b0ff213
PS
72 break;
73 default:
74 $text = empty($text) ? '' : ': '.$text;
75 $text = 'Unknown error '.$errorcode.$text;
76 break;
77 }
0d8e51a6
PS
78
79 // do not write to error stream because we need the error message in PHP exec result from web ui
80 echo($text."\n");
7b0ff213
PS
81 exit($errorcode);
82}
83
5f9ba165
PS
84/**
85 * Returns relative path against current working directory,
86 * to be used for shell execution hints.
87 * @param string $moodlepath starting with "/", ex: "/admin/tool/cli/init.php"
88 * @return string path relative to current directory or absolute path
89 */
90function phpunit_bootstrap_cli_argument_path($moodlepath) {
91 global $CFG;
92
93 if (isset($CFG->admin) and $CFG->admin !== 'admin') {
94 $moodlepath = preg_replace('|^/admin/|', "/$CFG->admin/", $moodlepath);
95 }
96
97 $cwd = getcwd();
98 if (substr($cwd, -1) !== DIRECTORY_SEPARATOR) {
99 $cwd .= DIRECTORY_SEPARATOR;
100 }
101 $path = realpath($CFG->dirroot.$moodlepath);
102
103 if (strpos($path, $cwd) === 0) {
880f8e7b
104 $path = substr($path, strlen($cwd));
105 }
106
107 if (phpunit_bootstrap_is_cygwin()) {
108 $path = str_replace('\\', '/', $path);
5f9ba165
PS
109 }
110
111 return $path;
112}
113
7b0ff213
PS
114/**
115 * Mark empty dataroot to be used for testing.
116 * @param string $dataroot The dataroot directory
117 * @return void
118 */
119function phpunit_bootstrap_initdataroot($dataroot) {
120 global $CFG;
121 umask(0);
122 if (!file_exists("$dataroot/phpunittestdir.txt")) {
123 file_put_contents("$dataroot/phpunittestdir.txt", 'Contents of this directory are used during tests only, do not delete this file!');
124 }
6e2cff2d 125 phpunit_boostrap_fix_file_permissions("$dataroot/phpunittestdir.txt");
7b0ff213
PS
126 if (!file_exists("$CFG->phpunit_dataroot/phpunit")) {
127 mkdir("$CFG->phpunit_dataroot/phpunit", $CFG->directorypermissions);
128 }
129}
6e2cff2d
PS
130
131/**
132 * Try to change permissions to $CFG->dirroot or $CFG->dataroot if possible
133 * @param string $file
134 * @return bool success
135 */
136function phpunit_boostrap_fix_file_permissions($file) {
137 global $CFG;
138
139 $permissions = fileperms($file);
140 if ($permissions & $CFG->filepermissions != $CFG->filepermissions) {
141 $permissions = $permissions | $CFG->filepermissions;
142 return chmod($file, $permissions);
143 }
144
145 return true;
920f4efe 146}
880f8e7b
147
148/**
149 * Find out if running under Cygwin on Windows.
150 * @return bool
151 */
152function phpunit_bootstrap_is_cygwin() {
153 if (empty($_SERVER['SHELL']) or empty($_SERVER['OS'])) {
154 return false;
155 }
156 return ($_SERVER['OS'] === 'Windows_NT' and $_SERVER['SHELL'] === '/bin/bash');
157}