def test_that_update_on_change_is_not_run_if_file_is_not_changed(tmpdir): with batman_dir({ "hash_dir": str(tmpdir), "update_on_change": { "monkey.txt": "touch bananas.txt" } }) as tmp_batman_dir: os.system('batman {0}'.format(tmp_batman_dir)) dirlisting = run('ls -la', in_dir=tmp_batman_dir).output os.system('batman {0}'.format(tmp_batman_dir)) assert run('ls -la', in_dir=tmp_batman_dir).output == dirlisting
def test_that_virtualenv_is_created(tmpdir): TEST_PYTHON_FILENAME = '__batman_testfile.py' TEST_VIRTUALENV = '__batman_testenv' test_python_file = os.path.join(str(tmpdir), TEST_PYTHON_FILENAME) with batman_dir({ "virtualenv": TEST_VIRTUALENV, "add2virtualenv": [str(tmpdir)], }) as tmp_batman_dir: touch_file(test_python_file) os.system('batman {0}'.format(tmp_batman_dir)) output = TEST_PYTHON_FILENAME in run('lssitepackages', virtualenv=TEST_VIRTUALENV).output run("rmvirtualenv {0}".format(TEST_VIRTUALENV))
def test_that_on_update_commands_still_get_rerun_if_file_is_updated(tmpdir): test_yml = { "hash_dir": str(tmpdir), "update_on_change": { "monkey.txt": "echo -ne '.' >> onedot.txt" } } with batman_dir(test_yml) as tmp_batman_dir: touch_file(os.path.join(tmp_batman_dir, 'monkey.txt')) os.system('batman {0}'.format(tmp_batman_dir)) run('echo -ne "updated" > monkey.txt', in_dir=tmp_batman_dir) os.system('batman {0}'.format(tmp_batman_dir)) assert run('cat onedot.txt', in_dir=tmp_batman_dir).output == '..'
def test_wildcard_expansion(tmpdir): with batman_dir({ "hash_dir": str(tmpdir), "update_on_change": { "*/migrations/*": "echo -ne '.' >> onedot.txt" } }) as tmp_batman_dir: testpath = os.path.join(tmp_batman_dir, 'testapp', 'migrations') os.makedirs(testpath) os.system('batman {0}'.format(tmp_batman_dir)) touch_file(os.path.join(testpath, 'imhere.txt')) os.system('batman {0}'.format(tmp_batman_dir)) run('echo "bleh" > {0}'.format(os.path.join(tmp_batman_dir, 'imhere.txt'))) assert run('cat onedot.txt', in_dir=tmp_batman_dir).output == '..'
def test_wildcard_expansion(tmpdir): with batman_dir({ "hash_dir": str(tmpdir), "update_on_change": { "*/migrations/*": "echo -ne '.' >> onedot.txt" } }) as tmp_batman_dir: testpath = os.path.join(tmp_batman_dir, 'testapp', 'migrations') os.makedirs(testpath) os.system('batman {0}'.format(tmp_batman_dir)) touch_file(os.path.join(testpath, 'imhere.txt')) os.system('batman {0}'.format(tmp_batman_dir)) run('echo "bleh" > {0}'.format( os.path.join(tmp_batman_dir, 'imhere.txt'))) assert run('cat onedot.txt', in_dir=tmp_batman_dir).output == '..'
def test_that_update_on_change_is_run_if_file_is_created(tmpdir): with batman_dir({ "hash_dir": str(tmpdir), "update_on_change": { "monkey.txt": "touch bananas.txt" } }) as tmp_batman_dir: os.system('batman {0}'.format(tmp_batman_dir)) touch_file(os.path.join(tmp_batman_dir, 'monkey.txt')) os.system('batman {0}'.format(tmp_batman_dir)) assert 'bananas.txt' in run('ls {0}'.format(tmp_batman_dir)).output
def check_and_update(stuff, hash_dir, basedir, virtualenv): #Only update changed keys (filter stuff to only include changed keys) changed_keys = dictchanged.changed_keys(stuff, hash_dir) for k, v in stuff.iteritems(): print "CHECKING {0}".format(k) need_to_run_command = file_has_changed(os.path.join(basedir, k), hash_dir or k in changed_keys) if need_to_run_command: print "{0} HAS CHANGED!".format(k) ret = run(v, virtualenv=virtualenv, in_dir=basedir, print_output=True) if ret.returncode == 0: # Command was successful update_hash(os.path.join(basedir, k), hash_dir) else: # Command failed print "FAILURE RUNNING COMMAND {0}".format(v) print ret.output
def test_that_on_update_commands_dont_get_rerun(tmpdir): """ Should keep track of yaml stuff on a key-by-key basis and only rerun commands if that specific piece has changed. """ test_yml = { "hash_dir": str(tmpdir), "update_on_change": { "monkey.txt": "echo -ne '.' >> onedot.txt" } } with batman_dir(test_yml) as tmp_batman_dir: touch_file(os.path.join(tmp_batman_dir, 'monkey.txt')) os.system('batman {0}'.format(tmp_batman_dir)) test_yml['update_on_change']['walrus.txt'] = 'touch bucket.txt' update_batman_yml(tmp_batman_dir, test_yml) os.system('batman {0}'.format(tmp_batman_dir)) assert run('cat onedot.txt', in_dir=tmp_batman_dir).output == '.'
def test_that_order_is_preserved(tmpdir): """ If the hashdir doesnt exist, create it. """ with batman_dir({ "hash_dir": str(tmpdir), "update_on_change": { "a": "echo -ne 'a' >> alpha.txt", "b": "echo -ne 'b' >> alpha.txt", "c": "echo -ne 'c' >> alpha.txt", "d": "echo -ne 'd' >> alpha.txt", "e": "echo -ne 'e' >> alpha.txt", "f": "echo -ne 'f' >> alpha.txt", "g": "echo -ne 'g' >> alpha.txt", "h": "echo -ne 'h' >> alpha.txt", } }) as tmp_batman_dir: os.system('batman {0}'.format(tmp_batman_dir)) assert run('cat alpha.txt', in_dir=tmp_batman_dir).output == 'abcdefgh'
def test_that_files_are_recursively_deleted(): with batman_dir({'delete_pattern': "*.poop"}) as tmp_batman_dir: os.mkdir(os.path.join(tmp_batman_dir, 'something')) touch_file(os.path.join(tmp_batman_dir, 'something', 'deleteme.poop')) os.system('batman {0}'.format(tmp_batman_dir)) assert 'deleteme' not in run('ls {0}'.format(tmp_batman_dir)).output
def test_that_bash_is_prepended(mr): run('ls') assert mr.call_args[0][0][0:2] == ['/usr/bin/env', 'bash']
def test_in_dir_calls_cd(mr): run('ls', in_dir='foo') assert mr.call_args[0][0][-1].endswith('cd foo && ls')
def test_workon_is_prepended(mr): run('ls', virtualenv='foo') assert mr.call_args[0][0][-1].endswith('workon foo && ls')
def test_that_virtualenv_is_created(): with batman_dir({"virtualenv": "__batman__testenv"}) as tmp_batman_dir: os.system('batman {0}'.format(tmp_batman_dir)) output = '__batman__testenv' in run('lsvirtualenv').output run("rmvirtualenv __batman__testenv") assert output