Skip to content

Commit

Permalink
Merge pull request #81 from PressLabs/paths_with_spaces
Browse files Browse the repository at this point in the history
Paths with spaces fixes #70
  • Loading branch information
rciorba committed Dec 29, 2015
2 parents 2cd2ef8 + ebef40e commit e15eef3
Show file tree
Hide file tree
Showing 13 changed files with 268 additions and 18 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/vendor/
composer.lock
.composer/cache
.vimrc
*.pyc
*~
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ env:
before_script:
- composer install --dev
- bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION
script: phpunit --tap --coverage-clover build/logs/clover.xml
script: ./vendor/bin/phpunit --tap --coverage-clover build/logs/clover.xml
after_script:
- bash bin/code-climate-coverage.sh
20 changes: 20 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.PHONY:

all: test

clean:
rm -rf /tmp/wordpress
# rm /tmp/wordpress.tar.gz /tmp/wordpress-*.tar.gz
rm -rf /tmp/wordpress-tests-lib
mysqladmin drop wordpress_test --user root --force

env_39:
./composer.phar install
bash ./bin/install-wp-tests.sh wordpress_test root '' localhost 3.9

env_latest:
./composer.phar install
bash ./bin/install-wp-tests.sh wordpress_test root '' localhost latest

test:
./vendor/bin/phpunit --tap
10 changes: 8 additions & 2 deletions bin/install-wp-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,15 @@ install_test_suite() {
# set up testing suite
mkdir -p $WP_TESTS_DIR
cd $WP_TESTS_DIR
svn co --quiet http://develop.svn.wordpress.org/trunk/tests/phpunit/includes/
if [ $WP_VERSION == 'latest' ]; then
local SVN_VERSION="trunk"
else
local SVN_VERSION="tags/${WP_VERSION}"
fi

svn co --quiet http://develop.svn.wordpress.org/$SVN_VERSION/tests/phpunit/includes/

wget -nv -O wp-tests-config.php http://develop.svn.wordpress.org/trunk/wp-tests-config-sample.php
wget -nv -O wp-tests-config.php http://develop.svn.wordpress.org/$SVN_VERSION/wp-tests-config-sample.php
sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR':" wp-tests-config.php
sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" wp-tests-config.php
sed $ioption "s/yourusernamehere/$DB_USER/" wp-tests-config.php
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"require-dev": {
"codeclimate/php-test-reporter": "dev-master",
"phpmd/phpmd" : "@stable"
"phpmd/phpmd" : "@stable",
"phpunit/phpunit": "4.8.*"
}
}
Binary file modified composer.phar
Binary file not shown.
19 changes: 14 additions & 5 deletions gitium/inc/class-git-wrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

class Git_Wrapper {

private $last_error = '';
private $gitignore = <<<EOF
define('GITIGNORE', <<<EOF
*.log
*.swp
*.back
Expand Down Expand Up @@ -87,8 +84,14 @@ class Git_Wrapper {
/wp-signup.php
/wp-trackback.php
/xmlrpc.php
EOF;
EOF
);


class Git_Wrapper {

private $last_error = '';
private $gitignore = GITIGNORE;
function __construct( $repo_dir ) {
$this->repo_dir = $repo_dir;
$this->private_key = '';
Expand Down Expand Up @@ -444,6 +447,12 @@ function get_local_changes() {
$y = substr( $item, 1, 1 ); // Y shows the status of the work tree
$file = substr( $item, 3 );

if ( ( '"' == $file[0] ) && ('"' == $file[strlen( $file ) - 1] ) ) {
// git status --porcelain will put quotes around paths with whitespaces
// we don't want the quotes, let's get rid of them
$file = substr( $file, 1, strlen( $file ) - 2 );
}

if ( 'D' == $y ) {
$action = 'deleted';
} else {
Expand Down
7 changes: 7 additions & 0 deletions test-env/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM ubuntu:latest
MAINTAINER Presslabs Engineering [email protected]

RUN apt-get -qq update
RUN DEBIAN_FRONTEND=noninteractive apt-get -yq install php5-cli php5-dev php5-curl php5-mysql git subversion wget mysql-client-5.5 mysql-server-5.5
ADD create_user.py /create_user.py
CMD ["/usr/bin/python3", "/create_user.py"]
64 changes: 64 additions & 0 deletions test-env/create_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from __future__ import print_function

import subprocess
import sys
import os
import grp


def shell(cmd, die=True, encoding='utf8'):
try:
# print(cmd)
out = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True)
# print(out)
# print("")
return str(out, encoding=encoding)
except subprocess.CalledProcessError as err:
# print(err.output)
# print("")
if die is True:
raise
else:
return str(err.output, encoding=encoding)


def get_bindmount_user_id():
out = shell('ls -lnd /code')
uid, gid = out.split()[2:4]
return uid, gid


def create_user():
uid, gid = get_bindmount_user_id()
username = 'developer'
groupname = 'developer'
try:
groupname = grp.getgrgid(gid).gr_name
print("group with id {} already exists: {}".format(gid, groupname))
except KeyError:
shell('groupadd developer --gid {}'.format(gid))
out = shell('id -un {}'.format(uid), die=False)
if out == 'id: {}: no such user\n'.format(uid):
print('creating user "developer"')
shell("useradd developer --home /code --uid {} --gid {} --shell=/bin/bash".format(
uid, gid
))
else:
username = out
print('user with id {} already exists: {}'.format(uid, username))
return username, groupname


def exec_to_bash():
username, groupname = create_user()
print("starting mysql")
shell("/etc/init.d/mysql start")
print("dropping you to an interactive shell as {}".format(username))
print("type CTRL+D to return to root shell")
os.chdir('/code')
sys.stdout.flush()
cmd = ["/bin/bash", "bash", "-c", "sudo -iu {}; bash".format(username)]
os.execl(*cmd)

if __name__ == '__main__':
exec_to_bash()
14 changes: 7 additions & 7 deletions tests/gitium-unittestcase.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

class Gitium_UnitTestCase extends WP_UnitTestCase {
protected $remote_repo = null;
protected $local_file = null;
protected $work_file = null;
protected $work_fname = 'work-file.txt';
protected $work_repo = '/tmp/gitium-repo';
protected $delete_on_teardown = array();
public $remote_repo = null;
public $local_file = null;
public $work_file = null;
public $work_fname = 'work-file.txt';
public $work_repo = null; // temp dir will be created by setup()
public $delete_on_teardown = array();

protected function _create_work_fresh_clone() {
if ( $this->work_repo ) {
Expand Down Expand Up @@ -105,7 +105,7 @@ public function teardown() {
// remove the files of the test
exec( "rm -rf {$this->local_file} ; rm -rf {$this->work_repo}" );
foreach ( $this->delete_on_teardown as $file ) {
exec( "rm -rf $file" );
exec( "rm -rf " . escapeshellarg( $file ) );
}
$this->delete_on_teardown = array();
}
Expand Down
65 changes: 65 additions & 0 deletions tests/repl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

/* Copyright 2014-2015 Presslabs SRL <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

/* A Read-Eval-Print-Loop - Poor man's interactive debugger.
usage: repl(get_defined_vars());
repl(get_defined_vars(), $this);
You can pass in multiple expressions separated by semicolons;
The value of the last expression will be returned.
*/
function repl($vars, $_this=null) {
// inject the variables in the current scope
// fwrite( STDOUT, "repl\n" );
if ($_this !== null) {
$vars['_this'] = $_this;
}
foreach($vars as $__name => $__value) {
fwrite( STDOUT, '$'.$__name.'=$vars["'.$__name.'"];' . "\n" );
if ($__name == 'this'){
continue;
}
eval('$'.$__name.'=$vars["'.$__name.'"];');
}
$__ln = 1;
while ( true ) {
try {
$__line = readline("repl $__ln> ");
if ($__line === false) # user presed ^D
break;
$__exploded = explode(";", $__line);
$__last = count($__exploded) - 1;
if ( $__exploded[$__last] === "" ) {
array_pop($__exploded);
$__last--;
}
$__exploded[$__last] = "return " . $__exploded[$__last];
$__cmd = "";
foreach($__exploded as $__statement) {
$__cmd = $__cmd . $__statement . ";" ;
}
fwrite( STDOUT, print_r( eval( $__cmd ), true ) . "\n" );
readline_add_history( $__line );
} catch (Exception $__err) {
fwrite( STDOUT, 'Exception: ' . $__err->getMessage() . "\n");
fwrite( STDOUT, "Traceback: \n". $__err->getTraceAsString() . "\n");
}
$__ln++;
}
}
60 changes: 60 additions & 0 deletions tests/test-git-wrapper.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

require_once 'gitium-unittestcase.php';
require_once 'repl.php';

class Test_Git_Wrapper extends Gitium_UnitTestCase {
/**
Expand Down Expand Up @@ -136,6 +137,7 @@ function test_get_local_changes() {
$this->assertEmpty( $git->get_local_changes() );
}


function test_get_last_error() {
global $git;
$this->assertEmpty( $git->get_last_error() );
Expand Down Expand Up @@ -233,4 +235,62 @@ function test_git_dir_constant() {
$this->assertTrue( defined( 'GIT_DIR' ) );
$this->assertEquals( GIT_DIR, $git->repo_dir );
}

/**
* Test paths with whitespaces get added and commited correctly
*/
function test_commit_path_with_whitespace() {
global $git;
$dir = $git->repo_dir . "/some dir/";
$this->delete_on_teardown[] = $dir;
try {
mkdir( $dir, 0777, true );
} catch (Exception $_) { }
file_put_contents( $dir . "some file", "ana are mere" );
$git->add();
$changeset = $git->commit( "add path with whitespace" );
// chdir( $git->repo_dir );
$output = explode( "\n", shell_exec( "cd {$git->repo_dir} ; git show --name-status $changeset" ) );
$expected = array(
" add path with whitespace",
"",
"A\tsome dir/some file");
$out = array_slice( $output, 4, -1 );
$this->assertEquals( $expected, $out );
}

/**
* Test is_dirty works for paths with whitespaces
*/
function test_is_dirty_with_whitespace() {
global $git;
$dir = $git->repo_dir . "/some dir/";
$this->delete_on_teardown[] = $dir;
try {
mkdir( $dir, 0777, true );
} catch (Exception $_) { }
file_put_contents( $dir . "some file", "ana are mere" );
$this->assertTrue( $git->is_dirty() );
}

/**
* Test get_local_changes for paths with whitespaces
*/
function test_get_local_changes_with_whitespace() {
global $git;
$dir = $git->repo_dir . "/some dir/";
$this->delete_on_teardown[] = $dir;
try {
mkdir( $dir, 0777, true );
} catch (Exception $_) { }
file_put_contents( $dir . "some file", "ana are mere" );
$git->add();
// repl(get_defined_vars(), $this);
$this->assertEquals(
['some dir/some file' => 'modified'],
$git->get_local_changes()
);
}


}
19 changes: 17 additions & 2 deletions tests/test-gitium.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

require_once("repl.php");

class Test_Gitium extends WP_UnitTestCase {
private $test_gitium_is_activated = false;
private $plugin = 'gitium/gitium.php';
Expand Down Expand Up @@ -128,13 +130,13 @@ function test_gitium_module_by_path_case_5() {
$this->assertTrue( $assert );
}

function test_gitium_module_by_path_case_6() {
function test_gitium_module_by_path_unregistered_theme() {
$path = 'wp-content/themes/mobile_pack_red/style.css.nokia.css';
$assert = _gitium_module_by_path( $path ) == array(
'base_path' => $path,
'type' => 'theme',
'name' => basename( $path ),
'version' => null,
'version' => null, # this theme is not in the themes transient, so we can't determine version
);
$this->assertTrue( $assert );
}
Expand All @@ -150,6 +152,19 @@ function test_gitium_module_by_path_case_7() {
$this->assertTrue( $assert );
}

function test_gitium_module_by_path_with_whitespaces() {
$path = 'wp-content/themes/hahaha/white space.css';
$module = _gitium_module_by_path( $path );

$expected = array(
'base_path' => 'wp-content/themes/hahaha',
'type' => 'theme',
'name' => 'Ha ha ha hi',
'version' => '0.0.1',
);
$this->assertEquals( $expected, $module );
}

/* '??' => 'untracked',
'rM' => 'modified to remote',
'rA' => 'added to remote',
Expand Down

0 comments on commit e15eef3

Please sign in to comment.