import os
import os.path
import contextlib
import tempfile
import subprocess
import shutil

from nose.tools import istest, assert_equal

from whack.builder import Builders

@istest
def can_build_simple_application():
    with _temporary_dir() as repo_dir, _temporary_dir() as install_dir:
        _create_test_builder(repo_dir)
        
        builders = Builders(should_cache=False, builder_repo_uris=[repo_dir])
        builders.build_and_install("hello=1", install_dir)
        
        output = subprocess.check_output([os.path.join(install_dir, "hello")])
        assert_equal("Hello there\n", output)
    
@contextlib.contextmanager
def _temporary_dir():
    try:
        build_dir = tempfile.mkdtemp()
        yield build_dir
    finally:
        shutil.rmtree(build_dir)

def _create_test_builder(repo_dir):
    builder_dir = os.path.join(repo_dir, "hello")
    os.makedirs(builder_dir)
    _write_script(os.path.join(builder_dir, "build"), _TEST_BUILDER_BUILD)
    _write_script(os.path.join(builder_dir, "install"), _TEST_BUILDER_INSTALL)
    
def _write_script(path, contents):
    _write_file(path, contents)
    _make_executable(path)

def _make_executable(path):
    subprocess.check_call(["chmod", "u+x", path])

def _write_file(path, contents):
    open(path, "w").write(contents)

_TEST_BUILDER_BUILD = r"""#!/bin/sh
INSTALL_DIR=$1
cat > hello << EOF
#!/bin/sh
echo Hello there
EOF

chmod +x hello
"""

_TEST_BUILDER_INSTALL = r"""#!/bin/sh
INSTALL_DIR=$1
cp hello $INSTALL_DIR/hello
"""

@@ -210,121 +210,1200 @@
 tion
-():%0A    with _temporary_dir() as repo_dir, _temporary_dir() as install_dir:%0A        _create_test_builder(repo_dir
+_with_install_dir_used_in_install_script():%0A    _TEST_BUILDER_BUILD = r%22%22%22#!/bin/sh%0AINSTALL_DIR=$1%0Acat %3E hello %3C%3C EOF%0A#!/bin/sh%0Aecho Hello there%0AEOF%0A%0Achmod +x hello%0A%22%22%22%0A%0A    _TEST_BUILDER_INSTALL = r%22%22%22#!/bin/sh%0AINSTALL_DIR=$1%0Acp hello $INSTALL_DIR/hello%0A%22%22%22%0A    with _temporary_dir() as repo_dir, _temporary_dir() as install_dir:%0A        _create_test_builder(repo_dir, _TEST_BUILDER_BUILD, _TEST_BUILDER_INSTALL)%0A        %0A        builders = Builders(should_cache=False, builder_repo_uris=%5Brepo_dir%5D)%0A        builders.build_and_install(%22hello=1%22, install_dir)%0A        %0A        output = subprocess.check_output(%5Bos.path.join(install_dir, %22hello%22)%5D)%0A        assert_equal(%22Hello there%5Cn%22, output)%0A%0A@istest%0Adef can_build_simple_application_with_install_dir_used_in_build_script():%0A    _TEST_BUILDER_BUILD = r%22%22%22#!/bin/sh%0AINSTALL_DIR=$1%0Aecho $INSTALL_DIR %3E install-dir%0Acat %3E hello %3C%3C EOF%0A#!/bin/sh%0Aecho Hello there%0AEOF%0A%0Achmod +x hello%0A%22%22%22%0A%0A    _TEST_BUILDER_INSTALL = r%22%22%22#!/bin/sh%0AINSTALL_DIR=%60cat install-dir%60%0Acp hello $INSTALL_DIR/hello%0A%22%22%22%0A    with _temporary_dir() as repo_dir, _temporary_dir() as install_dir:%0A        _create_test_builder(repo_dir, _TEST_BUILDER_BUILD, _TEST_BUILDER_INSTALL
 )%0A  
@@ -1886,16 +1886,32 @@
 repo_dir
+, build, install
 ):%0A    b
@@ -2038,35 +2038,21 @@
 uild%22), 
-_TEST_BUILDER_BUILD
+build
 )%0A    _w
@@ -2101,37 +2101,23 @@
 tall%22), 
-_TEST_BUILDER_INSTALL
+install
 )%0A    %0Ad
@@ -2362,212 +2362,4 @@
 ts)%0A
-%0A_TEST_BUILDER_BUILD = r%22%22%22#!/bin/sh%0AINSTALL_DIR=$1%0Acat %3E hello %3C%3C EOF%0A#!/bin/sh%0Aecho Hello there%0AEOF%0A%0Achmod +x hello%0A%22%22%22%0A%0A_TEST_BUILDER_INSTALL = r%22%22%22#!/bin/sh%0AINSTALL_DIR=$1%0Acp hello $INSTALL_DIR/hello%0A%22%22%22%0A

Add test that uses install_dir in build script