Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.

Commit 7f708b1

Browse files
author
Michael Grauer
committed
BUG: refs #68. better handling in exec function for chdir, plus testing.
1 parent b8d9ad4 commit 7f708b1

File tree

3 files changed

+69
-6
lines changed

3 files changed

+69
-6
lines changed

library/KWUtils.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,19 +141,35 @@ public static function appendStringIfNot($subject, $ext)
141141
*/
142142
public static function exec($command, &$output = null, $chdir = "", &$return_val = null)
143143
{
144-
$currCwd = getcwd();
145-
if(!empty($chdir) && is_dir($chdir))
144+
$changed = false;
145+
if(!empty($chdir))
146146
{
147-
if(!chdir($chdir))
147+
if(is_dir($chdir))
148148
{
149-
throw new Zend_Exception("Failed to change directory: [".$chdir."]");
149+
if(!getcwd())
150+
{
151+
throw new Exception ('getcwd failed');
152+
}
153+
$currCwd = getcwd();
154+
155+
if(!chdir($chdir))
156+
{
157+
throw new Zend_Exception("Failed to change directory: [".$chdir."]");
158+
}
159+
$changed = true;
160+
}
161+
else
162+
{
163+
throw new Zend_Exception("passed in chdir is not a directory: [".$chdir."]");
150164
}
151165
}
166+
152167
// on Linux need to add redirection to handle stderr
153168
$redirect_error = KWUtils::isLinux() ? " 2>&1" : "";
154169
exec(KWUtils::escapeCommand($command) . $redirect_error, $output, $return_val);
155-
// change back to original directory
156-
if(!chdir($currCwd))
170+
171+
// change back to original directory if necessary
172+
if($changed && !chdir($currCwd))
157173
{
158174
throw new Zend_Exception("Failed to change back to original directory: [".$currCwd."]");
159175
}

modules/batchmake/tests/controllers/components/KWBatchmakeComponentTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,16 @@ class KWBatchmakeComponentTest extends BatchmakeControllerTest
2323
protected $kwBatchmakeComponent;
2424
protected $applicationConfig;
2525

26+
27+
protected $cwd;
28+
2629
/** set up tests*/
2730
public function setUp()
2831
{
2932
$this->setupDatabase(array('default'));
3033
$this->_models = array('User');
3134
$this->enabledModules = array('batchmake');
35+
$this->cwd = getcwd();
3236
parent::setUp();
3337
if(!isset($this->kwBatchmakeComponent))
3438
{
@@ -46,6 +50,8 @@ public function tearDown()
4650
$testTmpDir = $this->getTempDirectory() . '/batchmake/tests';
4751

4852
KWUtils::recursiveRemoveDirectory($testTmpDir);
53+
// change the current dir back to the saved cwd after each test
54+
chdir($this->cwd);
4955
}
5056

5157

tests/library/KWUtilsTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ public function testExec()
6767
// not sure how to test this exactly, for now create a tmp dir, check
6868
// the value of pwd in it
6969

70+
$initialCwd = getcwd();
71+
$output = null;
72+
$returnVal = null;
73+
7074
// create a tmp dir for this test
7175
$execDir = KWUtils::getTempDirectory() . '/KWUtilsTest';
7276
mkdir($execDir);
@@ -75,6 +79,12 @@ public function testExec()
7579
KWUtils::exec($cmd, $output, $chdir, $returnVal);
7680
// $output should have one value, the same as execDir
7781

82+
$postCwd = getcwd();
83+
// check that we are back to the original directory after the exec
84+
// we are already checking with this test that we chdir correctly because
85+
// the test changes into a dir and then performs pwd there
86+
$this->assertEquals($initialCwd, $postCwd, "exec didn't correctly return to the origin directory");//
87+
7888
// yuck, need to do a bit of munging to get around tests/.. in BASE_PATH
7989
$execDir = str_replace('tests/../', '', $execDir);
8090
// and now replace any // with /,
@@ -87,6 +97,37 @@ public function testExec()
8797
$this->assertEquals($returnVal, 0);
8898
// now clean up the tmp dir
8999
rmdir($execDir);
100+
101+
// pass in a bad cwd, be sure that we get an exception
102+
$chdir = "/this/dir/probably/will/not/exist/anywhere";
103+
$output = null;
104+
$returnVal = null;
105+
try
106+
{
107+
KWUtils::exec($cmd, $output, $chdir, $returnVal);
108+
$this->fail();
109+
}
110+
catch(Zend_Exception $ze)
111+
{
112+
// this is the correct behavior
113+
$this->assertTrue(true);
114+
}
115+
$postCwd = getcwd();
116+
// ensure we are still in the same directory
117+
$this->assertEquals($initialCwd, $postCwd, "exec didn't correctly return to the origin directory");//
118+
119+
// now try to run pwd passing in a null chdir
120+
// should return an output the same as the initialCwd
121+
$chdir = null;
122+
$output = null;
123+
$returnVal = null;
124+
KWUtils::exec($cmd, $output, $chdir, $returnVal);
125+
// ensure that it ran in the initialCwd
126+
$this->assertEquals($initialCwd, $output[0]);
127+
128+
// check that we are still in the output dir
129+
$postCwd = getcwd();
130+
$this->assertEquals($initialCwd, $postCwd);
90131
}
91132

92133
/** tests appendStringIfNot function */

0 commit comments

Comments
 (0)