from StringIO import StringIO
from functools import partial
from unittest import TestCase, skip
import os

from bp.filepath import FilePath
from bp.memory import MemoryFS, MemoryPath

from mkenv import find
from mkenv.common import Locator


class TestFind(TestCase):
    def setUp(self):
        self.stdin = StringIO()
        self.stdout = StringIO()
        self.stderr = StringIO()

        self.fs = MemoryFS()
        self.locator = Locator(root=MemoryPath(fs=self.fs))

    def run_cli(self, argv=(), exit_status=os.EX_OK):
        find.run(
            argv=argv,
            stdin=self.stdin,
            stdout=self.stdout,
            stderr=self.stderr,
            exit=partial(self.assertEqual, exit_status),
            arguments={"locator" : self.locator},
        )
        return (
            self.stdin.getvalue(),
            self.stdout.getvalue(),
            self.stderr.getvalue(),
        )

    def test_find_without_args_finds_the_virtualenv_root(self):
        stdin, stdout, stderr = self.run_cli()
        self.assertEqual(
            (stdin, stdout, stderr),
            ("", self.locator.root.path + "\n", ""),
        )

    def test_find_d_finds_envs_by_directory(self):
        this_dir = FilePath(__file__).parent()
        stdin, stdout, stderr = self.run_cli(["-d", this_dir.path])
        self.assertEqual(
            (stdin, stdout, stderr),
            ("", self.locator.for_directory(this_dir).path + "\n", ""),
        )

    def test_find_d_defaults_to_cwd(self):
        stdin, stdout, stderr = self.run_cli(["-d"])
        self.assertEqual(
            (stdin, stdout, stderr),
            ("", self.locator.for_directory(FilePath(".")).path + "\n", ""),
        )

    def test_find_n_finds_envs_by_name(self):
        stdin, stdout, stderr = self.run_cli(["-n", "bla"])
        self.assertEqual(
            (stdin, stdout, stderr),
            ("", self.locator.for_name("bla").path + "\n", ""),
        )

    def test_find_existing_fails_for_non_existing_virtualenvs(self):
        stdin, stdout, stderr = self.run_cli(
            ["-n", "bla", "--existing-only"], exit_status=1,
        )
        self.assertEqual((stdin, stdout, stderr), ("", "", "")) 

    def test_find_existing_succeeds_for_existing_virtualenvs(self):
        MemoryPath(fs=self.fs, path=("bla",)).createDirectory()
        stdin, stdout, stderr = self.run_cli(["-n", "bla", "--existing-only"])
        self.assertEqual(
            (stdin, stdout, stderr),
            ("", self.locator.for_name("bla").path + "\n", ""),
        )

@@ -1980,16 +1980,24 @@
 xisting_
+by_name_
 fails_fo
@@ -2240,16 +2240,24 @@
 xisting_
+by_name_
 succeeds
@@ -2296,16 +2296,23 @@
 %0A       
+ path =
  MemoryP
@@ -2341,16 +2341,29 @@
 %22bla%22,))
+%0A        path
 .createD
@@ -2569,28 +2569,739 @@
 path + %22%5Cn%22, %22%22),%0A        )%0A
+%0A    def test_find_existing_by_dir_fails_for_non_existing_virtualenvs(self):%0A        stdin, stdout, stderr = self.run_cli(%0A            %5B%22-d%22, %22/foo/bla%22, %22--existing-only%22%5D, exit_status=1,%0A        )%0A        self.assertEqual((stdin, stdout, stderr), (%22%22, %22%22, %22%22))%0A%0A    def test_find_existing_by_dir_succeeds_for_existing_virtualenvs(self):%0A        MemoryPath(fs=self.fs, path=(%22bla%22,)).createDirectory()%0A        stdin, stdout, stderr = self.run_cli(%0A            %5B%22-d%22, %22/foo/bla%22, %22--existing-only%22%5D,%0A        )%0A        path = MemoryPath(fs=self.fs, path=(%22foo%22, %22bla%22,))%0A        self.assertEqual(%0A            (stdin, stdout, stderr),%0A            (%22%22, self.locator.for_directory(path).path + %22%5Cn%22, %22%22),%0A        )%0A

Add corresponding tests for by dir.