def test_activate_but_no_deactivate(venv_path, tmpdir, deactivate, shell): make_venv_in_tempdir(tmpdir) deactivate.remove() test = '''\ TEST> eval "$(aactivator init)" TEST> echo TEST> cd {venv_path} aactivator will source .activate.sh and .deactivate.sh at {venv_path}. Acceptable? (y)es (n)o (N)ever: INPUT> y aactivator will remember this: ~/.cache/aactivator/allowed aactivating... TEST> echo (aliased) TEST> cd / (aliased) aactivator: Cannot deactivate. File missing: {deactivate} TEST> echo (aliased) ''' test = test.format( venv_path=str(venv_path), deactivate=str(deactivate), ) run_test(shell, test, tmpdir)
def test_cd_dash(venv_path, tmpdir, shell): make_venv_in_tempdir(tmpdir) venv2 = make_venv_in_tempdir(tmpdir, 'venv2') test = '''\ TEST> eval "$(aactivator init)" TEST> cd {venv_path}/child-dir aactivator will source .activate.sh and .deactivate.sh at {venv_path}. Acceptable? (y)es (n)o (N)ever: INPUT> y aactivator will remember this: ~/.cache/aactivator/allowed aactivating... TEST> pwd {venv_path}/child-dir TEST> cd {venv2}/child-dir aactivator will source .activate.sh and .deactivate.sh at {venv2}. Acceptable? (y)es (n)o (N)ever: INPUT> y aactivator will remember this: ~/.cache/aactivator/allowed (aliased) deactivating... aactivating... TEST> cd - > /dev/null (aliased) deactivating... aactivating... TEST> pwd {venv_path}/child-dir TEST> cd - > /dev/null (aliased) deactivating... aactivating... TEST> pwd {venv2}/child-dir ''' test = test.format( venv_path=str(venv_path), venv2=str(venv2), ) run_test(shell, test, tmpdir)
def test_prompting_behavior(venv_path, tmpdir, shell): make_venv_in_tempdir(tmpdir) test = '''\ TEST> eval "$(aactivator init)" TEST> echo TEST> cd {venv_path} aactivator will source .activate.sh and .deactivate.sh at {venv_path}. Acceptable? (y)es (n)o (N)ever: INPUT> herpderp I didn't understand your response. Acceptable? (y)es (n)o (N)ever: INPUT> n TEST> echo TEST> echo TEST> echo TEST> cd / TEST> cd {venv_path} aactivator will source .activate.sh and .deactivate.sh at {venv_path}. Acceptable? (y)es (n)o (N)ever: INPUT> N aactivator will remember this: ~/.cache/aactivator/disallowed TEST> echo TEST> cd / TEST> echo ''' test = test.format(venv_path=str(venv_path)) run_test(shell, test, tmpdir)
def test_activate_but_no_deactivate(venv_path, tmpdir, deactivate, shell): make_venv_in_tempdir(tmpdir) deactivate.remove() test = '''\ TEST> eval "$(aactivator init)" TEST> echo TEST> cd {venv_path} aactivator will source .activate.sh and .deactivate.sh at {venv_path}. Acceptable? (y)es (n)o (N)ever: INPUT> y aactivator will remember this: ~/.cache/aactivator/allowed aactivating... TEST> echo (aliased) TEST> cd / (aliased) aactivator: Cannot deactivate. File missing: {deactivate} TEST> echo (aliased) ''' test = test.format( venv_path=str(venv_path), deactivate=str(deactivate), ) run_test(shell, test, tmpdir)
def test_eof_treated_like_no(tmpdir, venv_path, eof_config, allowed_config, disallowed_config): make_venv_in_tempdir(tmpdir) assert eof_config().find_allowed(str(venv_path)) is None # Saying no should not permanently save anything assert allowed_config.check(exists=0) assert disallowed_config.check(exists=0)
def test_prompting_behavior(venv_path, tmpdir, shell): make_venv_in_tempdir(tmpdir) test = '''\ TEST> eval "$(aactivator init)" TEST> echo TEST> cd {venv_path} aactivator will source .activate.sh and .deactivate.sh at {venv_path}. Acceptable? (y)es (n)o (N)ever: INPUT> herpderp I didn't understand your response. Acceptable? (y)es (n)o (N)ever: INPUT> n TEST> echo TEST> echo TEST> echo TEST> cd / TEST> cd {venv_path} aactivator will source .activate.sh and .deactivate.sh at {venv_path}. Acceptable? (y)es (n)o (N)ever: INPUT> N aactivator will remember this: ~/.cache/aactivator/disallowed TEST> echo TEST> cd / TEST> echo ''' test = test.format(venv_path=str(venv_path)) run_test(shell, test, tmpdir)
def test_cd_dash(venv_path, tmpdir, shell): make_venv_in_tempdir(tmpdir) venv2 = make_venv_in_tempdir(tmpdir, 'venv2') test = '''\ TEST> eval "$(aactivator init)" TEST> cd {venv_path}/child-dir aactivator will source .activate.sh and .deactivate.sh at {venv_path}. Acceptable? (y)es (n)o (N)ever: INPUT> y aactivator will remember this: ~/.cache/aactivator/allowed aactivating... TEST> pwd {venv_path}/child-dir TEST> cd {venv2}/child-dir aactivator will source .activate.sh and .deactivate.sh at {venv2}. Acceptable? (y)es (n)o (N)ever: INPUT> y aactivator will remember this: ~/.cache/aactivator/allowed (aliased) deactivating... aactivating... TEST> cd - > /dev/null (aliased) deactivating... aactivating... TEST> pwd {venv_path}/child-dir TEST> cd - > /dev/null (aliased) deactivating... aactivating... TEST> pwd {venv2}/child-dir ''' test = test.format( venv_path=str(venv_path), venv2=str(venv2), ) run_test(shell, test, tmpdir)
def test_get_output_already_sourced(tmpdir, venv_path, active_env): make_venv_in_tempdir(tmpdir) output = aactivator.get_output( dict(active_env), str(venv_path), lambda: 'y', ) assert output == ''
def test_get_output_sourced_deeper_in_directory(tmpdir, venv_path, active_env): make_venv_in_tempdir(tmpdir) deeper = venv_path.mkdir('deeper') output = aactivator.get_output( dict(active_env), str(deeper), lambda: 'y', ) assert output == ''
def test_get_output_pwd_goes_missing(tmpdir, venv_path, inactive_env): make_venv_in_tempdir(tmpdir) with venv_path.as_cwd(): venv_path.remove(rec=1) output = aactivator.get_output( dict(inactive_env), str(venv_path), lambda: 'n', ) assert output == ''
def test_not_sourced_sources(tmpdir, venv_path, inactive_env): make_venv_in_tempdir(tmpdir) output = aactivator.get_output( dict(inactive_env), str(venv_path), lambda: 'y', ) assert (output == '''\ aactivator security-check .activate.sh && source ./.activate.sh && export AACTIVATOR_ACTIVE=''' + str(venv_path))
def test_get_output_sourced_not_in_directory(tmpdir, venv_path, active_env): make_venv_in_tempdir(tmpdir) output = aactivator.get_output( dict(active_env), str(tmpdir), ) assert (output == '''\ OLDPWD_bak="$OLDPWD" && cd {venv_path} && aactivator security-check .deactivate.sh && source ./.deactivate.sh unset AACTIVATOR_ACTIVE && cd "$OLDPWD_bak" && cd {tmpdir} && unset OLDPWD_bak'''.format(venv_path=str(venv_path), tmpdir=str(tmpdir)))
def test_aactivator_goes_missing_no_output(venv_path, shell, tmpdir): make_venv_in_tempdir(tmpdir) exe = tmpdir.join('exe').strpath src = os.path.join(os.path.dirname(sys.executable), 'aactivator') shutil.copy(src, exe) test = '''\ TEST> eval "$({exe} init)" TEST> rm {exe} TEST> echo TEST> cd {venv_path} TEST> echo ''' test = test.format(venv_path=str(venv_path), exe=exe) run_test(shell, test, tmpdir)
def test_get_output_change_venv(tmpdir, venv_path, active_env): make_venv_in_tempdir(tmpdir) venv2 = make_venv_in_tempdir(tmpdir, 'venv2') output = aactivator.get_output( dict(active_env), str(venv2), lambda: 'y', ) assert (output == '''\ OLDPWD_bak="$OLDPWD" && cd {venv_path} && aactivator security-check .deactivate.sh && source ./.deactivate.sh unset AACTIVATOR_ACTIVE && cd "$OLDPWD_bak" && cd {venv_path}2 && unset OLDPWD_bak && aactivator security-check .activate.sh && source ./.activate.sh && export AACTIVATOR_ACTIVE={venv_path}2'''.format(venv_path=str(venv_path)))
def test_pwd_goes_missing(tmpdir, shell): tmpdir.mkdir('d') make_venv_in_tempdir(tmpdir) test = '''\ TEST> eval "$(aactivator init)" TEST> echo TEST> cd {{tmpdir}}/d TEST> rm -rf $PWD TEST> cd $PWD {pwd_missing} TEST> echo TEST> echo ''' test = test.format(pwd_missing=shell['errors']['pwd_missing'], ).format( tmpdir=str(tmpdir), ) run_test(shell, test, tmpdir)
def test_activates_when_cding_to_child_dir(venv_path, tmpdir, shell): make_venv_in_tempdir(tmpdir) test = '''\ TEST> eval "$(aactivator init)" TEST> echo TEST> cd {venv_path}/child-dir aactivator will source .activate.sh and .deactivate.sh at {venv_path}. Acceptable? (y)es (n)o (N)ever: INPUT> y aactivator will remember this: ~/.cache/aactivator/allowed aactivating... TEST> echo (aliased) TEST> cd / (aliased) deactivating... TEST> echo ''' test = test.format(venv_path=str(venv_path)) run_test(shell, test, tmpdir)
def test_activates_when_cding_to_child_dir(venv_path, tmpdir, shell): make_venv_in_tempdir(tmpdir) test = '''\ TEST> eval "$(aactivator init)" TEST> echo TEST> cd {venv_path}/child-dir aactivator will source .activate.sh and .deactivate.sh at {venv_path}. Acceptable? (y)es (n)o (N)ever: INPUT> y aactivator will remember this: ~/.cache/aactivator/allowed aactivating... TEST> echo (aliased) TEST> cd / (aliased) deactivating... TEST> echo ''' test = test.format(venv_path=str(venv_path)) run_test(shell, test, tmpdir)
def test_version_change(venv_path, tmpdir, shell): """If aactivator detects a version change, it will re-init and re-activate""" make_venv_in_tempdir(tmpdir) test = '''\ TEST> eval "$(aactivator init)" TEST> cd {venv_path} aactivator will source .activate.sh and .deactivate.sh at {venv_path}. Acceptable? (y)es (n)o (N)ever: INPUT> y aactivator will remember this: ~/.cache/aactivator/allowed aactivating... TEST> export AACTIVATOR_VERSION=0 aactivating... TEST> echo $AACTIVATOR_VERSION (aliased) {version} ''' test = test.format( venv_path=str(venv_path), version=aactivator.__version__, ) run_test(shell, test, tmpdir)
def test_version_change(venv_path, tmpdir, shell): """If aactivator detects a version change, it will re-init and re-activate""" make_venv_in_tempdir(tmpdir) test = '''\ TEST> eval "$(aactivator init)" TEST> cd {venv_path} aactivator will source .activate.sh and .deactivate.sh at {venv_path}. Acceptable? (y)es (n)o (N)ever: INPUT> y aactivator will remember this: ~/.cache/aactivator/allowed aactivating... TEST> export AACTIVATOR_VERSION=0 aactivating... TEST> echo $AACTIVATOR_VERSION (aliased) {version} ''' test = test.format( venv_path=str(venv_path), version=aactivator.__version__, ) run_test(shell, test, tmpdir)
def test_complains_parent_directory_insecure(venv_path, tmpdir, shell): make_venv_in_tempdir(tmpdir) venv_path.chmod(0o777) test = '''\ TEST> eval "$(aactivator init)" TEST> echo TEST> cd {venv_path} aactivator will source .activate.sh and .deactivate.sh at {venv_path}. Acceptable? (y)es (n)o (N)ever: INPUT> y aactivator will remember this: ~/.cache/aactivator/allowed aactivator: Cowardly refusing to source .activate.sh because writeable by others: . TEST> echo aactivator: Cowardly refusing to source .activate.sh because writeable by others: . TEST> cd / TEST> echo ''' test = test.format(venv_path=str(venv_path)) run_test(shell, test, tmpdir)
def test_no_is_remembered_until_cd_out(venv_path, tmpdir, no_config, yes_config): venv = make_venv_in_tempdir(tmpdir) assert no_config().find_allowed(str(venv_path)) is None assert yes_config().find_allowed(str(venv_path)) is None # cd to a child directory shouldn't forget the answer child_dir = venv.join('child-dir') assert yes_config().find_allowed(str(child_dir)) is None # cd to a parent directory directory (and back) should assert yes_config().find_allowed('/') is None assert yes_config().find_allowed(str(child_dir)) == str(venv_path)
def test_complains_when_not_activated(activate, venv_path, tmpdir, shell): make_venv_in_tempdir(tmpdir) activate.chmod(0o666) test = '''\ TEST> eval "$(aactivator init)" TEST> echo TEST> cd {venv_path} aactivator will source .activate.sh and .deactivate.sh at {venv_path}. Acceptable? (y)es (n)o (N)ever: INPUT> y aactivator will remember this: ~/.cache/aactivator/allowed aactivator: Cowardly refusing to source .activate.sh because writeable by others: .activate.sh TEST> echo aactivator: Cowardly refusing to source .activate.sh because writeable by others: .activate.sh TEST> cd / TEST> echo ''' test = test.format(venv_path=str(venv_path)) run_test(shell, test, tmpdir)
def test_activates_subshell(venv_path, tmpdir, shell): make_venv_in_tempdir(tmpdir) test = '''\ TEST> eval "$(aactivator init)" TEST> echo TEST> cd {venv_path} aactivator will source .activate.sh and .deactivate.sh at {venv_path}. Acceptable? (y)es (n)o (N)ever: INPUT> y aactivator will remember this: ~/.cache/aactivator/allowed aactivating... TEST> echo (aliased) TEST> {shell} TEST> echo TEST> eval "$(aactivator init)" aactivating... TEST> echo 2 (aliased) 2 TEST> cd / (aliased) deactivating... TEST> echo 3 3 TEST> exit 2>/dev/null TEST> echo (aliased) TEST> cd / (aliased) deactivating... TEST> echo 5 5 ''' test = test.format( venv_path=str(venv_path), shell=shellquote(shell['cmd']), ) run_test(shell, test, tmpdir)
def test_activates_subshell(venv_path, tmpdir, shell): make_venv_in_tempdir(tmpdir) test = '''\ TEST> eval "$(aactivator init)" TEST> echo TEST> cd {venv_path} aactivator will source .activate.sh and .deactivate.sh at {venv_path}. Acceptable? (y)es (n)o (N)ever: INPUT> y aactivator will remember this: ~/.cache/aactivator/allowed aactivating... TEST> echo (aliased) TEST> {shell} TEST> echo TEST> eval "$(aactivator init)" aactivating... TEST> echo 2 (aliased) 2 TEST> cd / (aliased) deactivating... TEST> echo 3 3 TEST> exit 2>/dev/null TEST> echo (aliased) TEST> cd / (aliased) deactivating... TEST> echo 5 5 ''' test = test.format( venv_path=str(venv_path), shell=shellquote(shell['cmd']), ) run_test(shell, test, tmpdir)
def test_pwd_goes_missing(tmpdir, shell): tmpdir.mkdir('d') make_venv_in_tempdir(tmpdir) test = '''\ TEST> eval "$(aactivator init)" TEST> echo TEST> cd {{tmpdir}}/d TEST> rm -rf $PWD TEST> cd $PWD {pwd_missing} TEST> echo TEST> echo ''' test = test.format( pwd_missing=shell['errors']['pwd_missing'], ).format( tmpdir=str(tmpdir), ) run_test(shell, test, tmpdir)
def test_not_owned_by_me_is_not_activatable(tmpdir, activate, yes_config): make_venv_in_tempdir(tmpdir) assert yes_config().is_allowed(str(activate), _getuid=lambda: -1) is False
def test_never_is_remembered(tmpdir, venv_path, never_config, yes_config): make_venv_in_tempdir(tmpdir) assert never_config().find_allowed(str(venv_path)) is None assert yes_config().find_allowed(str(venv_path)) is None
def test_prompt_loop_answer_yes(tmpdir, venv_path, yes_config, allowed_config): make_venv_in_tempdir(tmpdir) assert yes_config().find_allowed(str(venv_path)) == str(venv_path) assert allowed_config.check(file=1) assert allowed_config.read() == venv_path + '\n'
def test_yes_config_is_remembered(tmpdir, venv_path, yes_config, no_config): make_venv_in_tempdir(tmpdir) assert yes_config().find_allowed(str(venv_path)) == str(venv_path) assert no_config().find_allowed(str(venv_path)) == str(venv_path)
def test_never_config_not_allowed(tmpdir, venv_path, never_config, allowed_config, disallowed_config): make_venv_in_tempdir(tmpdir) assert never_config().find_allowed(str(venv_path)) is None assert disallowed_config.check(file=1) assert disallowed_config.read() == str(venv_path) + '\n'