import tempfile
import os
import shutil
from pathlib import Path

import pytest

from pew._utils import temp_environ, invoke_pew as invoke
from utils import skip_windows, xfail_nix, TemporaryDirectory


def test_project_dont_exist(env1):
    out = invoke('setproject', 'env1', 'i/dont/exist')
    assert out.err


def check_project_dir(workon_home, env, projdir):
    return (workon_home / env / '.project').open().read() == projdir


@skip_windows(reason="empty stdin on windows doesn't close the subprocess used by workon")
def test_implicit(workon_home, env1):
    "use the current virtualenv to associate the cwd as project directory"
    with TemporaryDirectory() as tmpdir:
        res = invoke('workon', 'env1', inp='pew setproject', cwd=tmpdir)
        assert check_project_dir(workon_home, 'env1', tmpdir)


@xfail_nix
def test_implicit_project(workon_home, env1):
    "use the cwd as project directory"
    with temp_environ():
        os.environ.pop('VIRTUAL_ENV', None)
        with TemporaryDirectory() as tmpdir:
            res = invoke('setproject', 'env1', cwd=tmpdir)
            assert not res.err
            assert check_project_dir(workon_home, 'env1', tmpdir)


@xfail_nix
def test_setproject(workon_home, env1):
    with temp_environ():
        os.environ.pop('VIRTUAL_ENV', None)
        with TemporaryDirectory() as tmpdir:
            res = invoke('setproject', 'env1', tmpdir)
            assert not res.err
            assert check_project_dir(workon_home, 'env1', tmpdir)


@skip_windows(reason="empty stdin on windows doesn't close the subprocess used by workon")
@xfail_nix
def test_ignore_environ(workon_home, env1, env2):
    with temp_environ():
        os.environ.pop('VIRTUAL_ENV', None)
        with TemporaryDirectory() as tmpdir:
            res = invoke('workon', 'env1', inp='pew setproject env2 ' + tmpdir)
            assert not (workon_home / 'env1' / '.project').exists()
            assert check_project_dir(workon_home, 'env2', tmpdir)


@skip_windows(reason="empty stdin on windows doesn't close the subprocess used by workon")
def test_workon_when_project_is_relative(env1):
    with TemporaryDirectory() as tmpdir:
        tmpdir = Path(tmpdir)
        invoke('setproject', 'env1', tmpdir.name, cwd=str(tmpdir.parent))
        workon_projdir = invoke('workon', 'env1', inp='pwd').out.strip()
        assert workon_projdir == str(tmpdir)

@@ -167,19 +167,8 @@
 ows,
- xfail_nix,
  Tem
@@ -183,16 +183,16 @@
 rectory%0A
+
 %0A%0Adef te
@@ -795,35 +795,24 @@
 , tmpdir)%0A%0A%0A
-@xfail_nix%0A
 def test_imp
@@ -1156,27 +1156,16 @@
 pdir)%0A%0A%0A
-@xfail_nix%0A
 def test
@@ -1456,32 +1456,32 @@
 nv1', tmpdir)%0A%0A%0A
+
 @skip_windows(re
@@ -1559,19 +1559,8 @@
 n%22)%0A
-@xfail_nix%0A
 def 

Remove xfails: fc0e98d made some tests pass again on Nix