import os
import sys

from spec import Spec, skip, eq_, raises

from invoke.loader import FilesystemLoader as Loader # TODO: expand
from invoke.collection import Collection
from invoke.exceptions import CollectionNotFound

from _utils import support


class Loader_(Spec):
    def exposes_discovery_start_point(self):
        start = '/tmp/'
        eq_(Loader(start=start).start, start)

    def has_a_default_discovery_start_point(self):
        eq_(Loader().start, os.getcwd())

    class load:
        def setup(self):
            self.l = Loader(start=support)

        def returns_collection_object_if_name_found(self):
            result = self.l.load('foo')
            eq_(type(result), Collection)

        @raises(CollectionNotFound)
        def raises_CollectionNotFound_if_not_found(self):
            self.l.load('nope')

        @raises(ImportError)
        def raises_ImportError_if_found_collection_cannot_be_imported(self):
            # Instead of masking with a CollectionNotFound
            self.l.load('oops')

        def searches_towards_root_of_filesystem(self):
            # Loaded while root is in same dir as .py
            directly = self.l.load('foo')
            # Loaded while root is multiple dirs deeper than the .py
            deep = os.path.join(support, 'ignoreme', 'ignoremetoo')
            indirectly = Loader(start=deep).load('foo')
            eq_(directly, indirectly)

        def defaults_to_tasks_collection(self):
            "defaults to 'tasks' collection"
            result = Loader(start=support + '/implicit/').load()
            eq_(type(result), Collection)

    class find:
        @raises(CollectionNotFound)
        def raises_CollectionNotFound_for_missing_collections(self):
            result = Loader(start=support).find('nope')

    class update_path:
        def setup(self):
            self.l = Loader(start=support)

        def does_not_modify_argument(self):
            path = []
            new_path = self.l.update_path(path)
            eq_(path, [])
            assert len(new_path) > 0

        def inserts_self_start_parent_at_front_of_path(self):
            "Inserts self.start at front of path"
            eq_(self.l.update_path([])[0], self.l.start)

        def adds_to_front_if_exists(self):
            "Inserts self.start at front of path even if it's already elsewhere"
            new_path = self.l.update_path([self.l.start])
            eq_(len(new_path), 2) # lol ?
            eq_(new_path[0], self.l.start)

@@ -1785,24 +1785,132 @@
 nd('nope')%0A%0A
+%0A#class SysPathLoader_(Spec):%0A#    # TODO: factor out anything that applies, from FilesystemLoader tests%0A#%0A#
     class up
@@ -1920,16 +1920,17 @@
 e_path:%0A
+#
         
@@ -1938,32 +1938,33 @@
 ef setup(self):%0A
+#
             self
@@ -1982,33 +1982,35 @@
 (start=support)%0A
-%0A
+#%0A#
         def does
@@ -2029,32 +2029,33 @@
 argument(self):%0A
+#
             path
@@ -2060,16 +2060,17 @@
 th = %5B%5D%0A
+#
         
@@ -2109,16 +2109,17 @@
 h(path)%0A
+#
         
@@ -2136,16 +2136,17 @@
 th, %5B%5D)%0A
+#
         
@@ -2174,17 +2174,19 @@
 th) %3E 0%0A
-%0A
+#%0A#
         
@@ -2231,32 +2231,33 @@
 _of_path(self):%0A
+#
             %22Ins
@@ -2290,16 +2290,17 @@
 f path%22%0A
+#
         
@@ -2344,25 +2344,27 @@
 lf.l.start)%0A
-%0A
+#%0A#
         def 
@@ -2386,32 +2386,33 @@
 f_exists(self):%0A
+#
             %22Ins
@@ -2476,16 +2476,17 @@
 ewhere%22%0A
+#
         
@@ -2535,16 +2535,17 @@
 start%5D)%0A
+#
         
@@ -2578,16 +2578,17 @@
 # lol ?%0A
+#
         

Cut out the sys.path related tests for now