Example #1
0
def test_cd_enters_a_path_and_returns_a_node_representing_it_abs(exists):
    ("Node#cd should return a node representing the given path. "
     "Testing with absolute path")

    nd = Node("/foo/bar/")
    other = nd.cd("/etc/")
    other.path.should.equal('/etc')
Example #2
0
def test_node_depth_of_with_file(isfile):
    ("Node#depth_of(path) should return the approriate number")

    isfile.return_value = True
    Node("/foo/bar/").depth_of("/foo/bar/another/dir/file.py").should.equal(2)
    Node("/foo/bar").depth_of("/foo/bar/another/dir/file.py").should.equal(2)
    Node("/foo/bar//").depth_of("/foo/bar/another/dir/file.py").should.equal(2)
Example #3
0
def test_open(io):
    ("Node#open should return an open file")
    io.open.return_value = 'an open file'

    nd = Node('/foo/bar')

    nd.open('wee.py').should.equal('an open file')
    io.open.assert_called_once_with('/foo/bar/wee.py')
Example #4
0
def test_node_could_be_updated_by_false(exists):
    ("Node#could_be_updated_by returns False if given "
     "node has an older modification time")

    nd = Node(__file__)
    nd.metadata.mtime = 1000
    other = Mock()
    other.metadata.mtime = 0
    nd.could_be_updated_by(other).should.be.false
Example #5
0
def test_contains_checks_if_path_exists(exists):
    ("Node#cd should return a node representing the given path.")

    exists.return_value = True
    nd = Node("/foo/bar/")
    nd.contains("file.py").should.be.truthy
    exists.assert_has_calls([
        call('/foo/bar/file.py'),
    ])
    exists.call_count.should.equal(1)
Example #6
0
def test_trip_at_when_not_lazy_relative(os):
    ("Node#trip_at(path, lazy=False) returns a list when lazy=False "
     "(testing with relative path)")
    os.walk.return_value = [
        ("/foo/bar/somewhere", [], ["file1.py", "file2.py"]),
    ]

    nd = Node("/foo/bar/")
    nd.trip_at('somewhere', lazy=False).should.equal([
        "/foo/bar/somewhere/file1.py",
        "/foo/bar/somewhere/file2.py",
    ])
Example #7
0
def test_trip_at_when_not_lazy_absolute(os):
    ("Node#trip_at(path, lazy=False) returns a list when lazy=False "
     "(testing with absolute path)")
    os.walk.return_value = [
        ("/dummy/", [], ["file1.py", "file2.py"]),
    ]

    nd = Node("/foo/bar/")
    nd.trip_at('/dummy/', lazy=False).should.equal([
        "/dummy/file1.py",
        "/dummy/file2.py",
    ])
Example #8
0
def test_node_list(os, isdir, dirname):
    ("Node#list should return a list containing one node "
     "per file found inside of the current node")

    dirname.return_value = '/foo/bar/items'
    isdir.return_value = False
    os.listdir.return_value = ['/foo/bar/items/whatever.py']

    nd = Node('/foo/bar/items/')
    nd.list().should.have.length_of(1)

    os.listdir.assert_called_once_with('/foo/bar/items')
Example #9
0
def test_trip_at_when_lazy_relative(os):
    ("Node#trip_at(path, lazy=True) returns a generator when lazy=True "
     "(testing with relative path)")
    os.walk.return_value = [
        ("/dummy/", [], ["file1.py", "file2.py"]),
    ]

    nd = Node("/foo/bar/")
    nd.trip_at('/dummy/', lazy=True).should.be.a('types.GeneratorType')
    list(nd.trip_at('/dummy/', lazy=True)).should.equal([
        "/dummy/file1.py",
        "/dummy/file2.py",
    ])
Example #10
0
def test_find_find_with_regexs_and_get_the_first_one_none(exists):
    ('Node#find returns the None if nothing is found')
    nd = Node('/foo/bar')
    nd.walk = Mock()
    nd.walk.return_value = [
        "/foo/wisdom/aaa.py",
        "/foo/wisdom/bbb.txt",
        "/foo/wisdom/ccc.php",
        "/foo/wisdom/ddd.py",
    ]
    ret = nd.find('^$')
    ret.should.be.none
    nd.walk.assert_called_once_with(lazy=True)
Example #11
0
def test_find_find_with_regexs_and_get_the_first_one(exists):
    ('Node#find returns the first result from find_with_regex when found')
    nd = Node('/foo/bar')
    nd.walk = Mock()
    nd.walk.return_value = [
        "/foo/wisdom/aaa.py",
        "/foo/wisdom/bbb.txt",
        "/foo/wisdom/ccc.php",
        "/foo/wisdom/ddd.py",
    ]
    ret = nd.find('[.]\w{3}$')
    ret.should.be.a(Node)
    ret.should.equal(Node("/foo/wisdom/bbb.txt"))
    nd.walk.assert_called_once_with(lazy=True)
Example #12
0
def test_open_dir_with_default_path(io):
    ("Node#open should raise error when called with default path on a dir")
    io.open.return_value = 'an open file'

    nd = Node('/foo/bar')

    nd.open.when.called_with().should.throw(TypeError)
    io.open.assert_not_called()
Example #13
0
def test_open_file_with_default_path(io):
    ("Node#open should default to self.path when called with default path on a file"
     )
    io.open.return_value = 'an open file'

    nd = Node('/foo/bar.py')
    nd.open.when.called_with().should.have.returned_the_value('an open file')

    io.open.assert_called_once_with('/foo/bar.py')
Example #14
0
def test_walk_trips_at_node_path():
    ("Node#walk() trips at node.path")
    nd = Node("/foo/bar/")
    nd.trip_at = Mock()

    nd.walk()
    nd.walk(lazy=True)

    nd.trip_at.assert_has_calls([
        call('/foo/bar', lazy=False),
        call('/foo/bar', lazy=True),
    ])
Example #15
0
def test_glob_filters_results_from_walk_using_fnmatch(exists):
    ('Node#glob returns a lazy list of nodes')
    nd = Node('/foo/bar')
    nd.walk = Mock()
    nd.walk.return_value = [
        "/foo/wisdom/aaa.py",
        "/foo/wisdom/bbb.txt",
        "/foo/wisdom/ccc.php",
        "/foo/wisdom/ddd.py",
    ]
    ret = nd.glob('*.py', lazy='passed-to-walk')
    ret.should.be.a('types.GeneratorType')
    list(ret).should.equal([
        Node("/foo/wisdom/aaa.py"),
        Node("/foo/wisdom/ddd.py"),
    ])
    nd.walk.assert_called_once_with(lazy='passed-to-walk')
Example #16
0
def test_glob_filters_results_from_walk_using_fnmatch_nonlazy(exists):
    ('Node#glob returns an evaluated list of nodes')
    nd = Node('/foo/bar')
    nd.walk = Mock()
    nd.walk.return_value = [
        "/foo/wisdom/aaa.py",
        "/foo/wisdom/bbb.txt",
        "/foo/wisdom/ccc.php",
        "/foo/wisdom/ddd.py",
    ]
    ret = nd.glob('*.py', lazy=False)
    ret.should.be.a(list)
    ret.should.equal([
        Node("/foo/wisdom/aaa.py"),
        Node("/foo/wisdom/ddd.py"),
    ])
    nd.walk.assert_called_once_with(lazy=False)
Example #17
0
def test_find_with_regex_filters_results_from_walk_using_regex_nonlazy(exists):
    ('Node#find_with_regex returns an evaluated list of nodes')
    nd = Node('/foo/bar')
    nd.walk = Mock()
    nd.walk.return_value = [
        "/foo/wisdom/aaa.py",
        "/foo/wisdom/bbb.txt",
        "/foo/wisdom/ccc.php",
        "/foo/wisdom/ddd.py",
    ]
    ret = nd.find_with_regex('[.]\w{3}$', lazy=False)
    ret.should.be.a(list)
    ret.should.equal([
        Node("/foo/wisdom/bbb.txt"),
        Node("/foo/wisdom/ccc.php"),
    ])
    nd.walk.assert_called_once_with(lazy=False)
Example #18
0
def test_node_relative(exists):
    ("Node#relative() returns a path relative to the base")

    nd = Node("/foo/bar/")
    nd.relative('/foo/bar/yes.py').should.equal('yes.py')
Example #19
0
def test_cd_enters_a_path_and_returns_a_node_representing_it(exists):
    ("Node#cd should return a node representing the given path")
    nd = Node("/foo/bar/")
    other = nd.cd("awesome/")
    other.path.should.equal('/foo/bar/awesome')
Example #20
0
def test_node_basename():
    ("Node#basename should return the basename for the node.path")
    nd = Node(__file__)
    nd.basename.should.equal('test_node.py')
Example #21
0
def test_node_path_to_related(exists):
    ("Node#path_to_related takes a path and returns the relative way there")
    nd = Node("/foo/bar/something.py")
    result = nd.path_to_related("/foo/docs/assets/style.css")
    result.should.equal('../../bar/something.py')
Example #22
0
def test_node_dir_when_is_dir():
    ("Node#dir should return itself when the current "
     "node is pointing to a file")

    nd = Node('/foo/bar/items/')
    nd.dir.path.should.equal('/foo/bar/items')
Example #23
0
def test_node_dir_when_is_file():
    ("Node#dir should return a node pointing to the parent "
     "dir when the current node is pointing to a file")

    nd = Node('/foo/bar/items/some.py')
    nd.dir.path.should.equal('/foo/bar/items')