Skip to content

Commit

Permalink
Fix changing user (#85)
Browse files Browse the repository at this point in the history
* Test autoload from src and bin perspective.

* Added Vagrant as a development environment.

* Added NotImplemented exception.

* Fixed changing user.
  • Loading branch information
PabloKowalczyk authored Apr 21, 2018
1 parent 89726a7 commit 814f8f8
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ composer.lock
*.log
.idea/
var/
.vagrant/
63 changes: 63 additions & 0 deletions Vagrantfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :

$script = <<SCRIPT
export DEBIAN_FRONTEND=noninteractive;
PHP_VERSION="5.6";
## Add Onrej PPA for php 5.6 - 7.2
add-apt-repository -y -u ppa:ondrej/php;
apt-get install git \
make \
"php$PHP_VERSION" \
"php$PHP_VERSION-cli" \
"php$PHP_VERSION-common" \
"php$PHP_VERSION-zip" \
"php$PHP_VERSION-mbstring" \
"php$PHP_VERSION-opcache" \
"php$PHP_VERSION-xml" \
"php$PHP_VERSION-intl" -y
SCRIPT

$composer = <<SCRIPT
COMPOSER_VERSION="1.6.4";
BIN_DIR="/home/ubuntu/bin";
COMPOSER_FILE="$BIN_DIR/composer";
mkdir -p "$BIN_DIR" &&
wget -q -O "$COMPOSER_FILE" "https://getcomposer.org/download/$COMPOSER_VERSION/composer.phar" &&
chmod +x "$COMPOSER_FILE";
SCRIPT

Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/xenial64"

config.vm.network :private_network, ip: "192.168.3.25"

config.vm.synced_folder ".", "/vagrant",
mount_options: ["actimeo=1"],
nfs: true,
linux__nfs_options: ["rw", "no_subtree_check", "all_squash", "async"]

if Vagrant::Util::Platform.windows? then
config.winnfsd.uid = 1000
config.winnfsd.gid = 1000
end

config.vm.provider "virtualbox" do |vb|
vb.memory = "1024"
vb.cpus = 2
vb.name = "crunz-dev"
end

config.vm.provision "shell", inline: $script
config.vm.provision "shell", inline: $composer
end
22 changes: 19 additions & 3 deletions crunz
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
|
*/

$autoloadFile = \implode(
$autoloadSrc = \implode(
DIRECTORY_SEPARATOR,
[
__DIR__,
Expand All @@ -22,12 +22,28 @@ $autoloadFile = \implode(
]
);

require $autoloadFile;
$autoloadBin = \implode(
DIRECTORY_SEPARATOR,
[
__DIR__,
'..',
'..',
'autoload.php'
]
);

$hasBinAutoloader = \file_exists($autoloadBin);

if ($hasBinAutoloader) {
require_once $autoloadBin;
} else {
require_once $autoloadSrc;
}

define('CRUNZ_ROOT', __DIR__);

// Setting the base directory as an environment variable
setbase(getroot($autoloadFile));
setbase(getroot($hasBinAutoloader ? $autoloadBin : $autoloadSrc));

// Setting the handler for catching/logging fatal and parse errors.
Crunz\ErrorHandler::getInstance()->set();
Expand Down
11 changes: 8 additions & 3 deletions src/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use Carbon\Carbon;
use Cron\CronExpression;
use Crunz\Exception\NotImplementedException;
use GuzzleHttp\Client as HttpClient;
use Symfony\Component\Process\Process;
use SuperClosure\Serializer;
Expand Down Expand Up @@ -210,7 +211,7 @@ public function buildCommand()

if ($this->cwd) {
if($this->user) {
$command .= $this->sudo();
$command .= $this->sudo($this->user);
}

// Support changing drives in Windows
Expand All @@ -221,7 +222,7 @@ public function buildCommand()
}

if ($this->user) {
$this->sudo();
$command .= $this->sudo($this->user);
}

$command .= $this->isClosure() ? $this->serializeClosure($this->command) : $this->command;
Expand All @@ -238,7 +239,7 @@ public function buildCommand()
*/
protected function sudo($user)
{
return 'sudo -u' . $user . ' ';
return "sudo -u {$user} ";
}

/**
Expand Down Expand Up @@ -741,6 +742,10 @@ public function timezone($timezone)
*/
public function user($user)
{
if ($this->isWindows()) {
throw new NotImplementedException('Changing user on Windows is not implemented.');
}

$this->user = $user;

return $this;
Expand Down
7 changes: 7 additions & 0 deletions src/Exception/NotImplementedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Crunz\Exception;

class NotImplementedException extends CrunzException
{
}
40 changes: 40 additions & 0 deletions tests/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,46 @@ public function onDoNotRunTaskEveryMinute()
$this->assertSame('0 8 * * * *', $event->getExpression());
}

/** @test */
public function settingUserPrependSudoToCommand()
{
$event = new Event($this->id, 'php -v');

$event->user('john');

$this->assertSame('sudo -u john php -v', $event->buildCommand());
}

/** @test */
public function customUserAndCwd()
{
if ($this->isWindows()) {
$this->markTestSkipped('Required Unix-based OS.');
}

$event = new Event($this->id, 'php -i');

$event->user('john');
$event->in('/var/test');

$this->assertSame('sudo -u john cd /var/test; sudo -u john php -i', $event->buildCommand());
}

/** @test */
public function notImplementedUserChangeOnWindows()
{
if (!$this->isWindows()) {
$this->markTestSkipped('Required Windows OS.');
}

$this->expectException(\Crunz\Exception\NotImplementedException::class);
$this->expectExceptionMessage('Changing user on Windows is not implemented.');

$event = new Event($this->id, 'php -i');

$event->user('john');
}

private function isWindows()
{
return DIRECTORY_SEPARATOR === '\\';
Expand Down

0 comments on commit 814f8f8

Please sign in to comment.