From 0f9435a7d888aa5fbb97f469a80f1e50e1755d3b Mon Sep 17 00:00:00 2001 From: andrews Date: Tue, 15 Sep 2009 11:21:13 -0300 Subject: [PATCH] Filesystem tests. --- .gitignore | 1 + Makefile | 3 + staticgenerator/filesystem.py | 10 +- .../tests/functional/test_filesystem.py | 139 ++++++++++++++++++ 4 files changed, 149 insertions(+), 4 deletions(-) create mode 100644 staticgenerator/tests/functional/test_filesystem.py diff --git a/.gitignore b/.gitignore index 29a43b1..599ccdf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *~ *.pyc .coverage +test_data diff --git a/Makefile b/Makefile index 6c497e5..2264420 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,8 @@ clean: @rm -rf build @echo "removing (.coverage)" @rm -f .coverage + @echo "removing (test_data)" + @rm -rf `pwd`/test_data @echo "Done!" unit: clean @@ -14,6 +16,7 @@ unit: clean functional: clean @echo "Running unit tests..." + @mkdir `pwd`/test_data @export PYTHONPATH=`pwd`:`pwd`/staticgenerator::$$PYTHONPATH && \ nosetests -d -s --verbose --with-coverage --cover-inclusive --cover-package=staticgenerator \ staticgenerator/tests/functional diff --git a/staticgenerator/filesystem.py b/staticgenerator/filesystem.py index 21e8465..b22bbaa 100644 --- a/staticgenerator/filesystem.py +++ b/staticgenerator/filesystem.py @@ -15,7 +15,7 @@ def tempfile(self, directory): return tempfile.mkstemp(dir=directory) def write(self, f, content): - os.write(f, content) + return os.write(f, content) def close(self, f): os.close(f) @@ -24,7 +24,7 @@ def chmod(self, filename, flags): os.chmod(filename, flags) def rename(self, from_file, to_file): - os.rename(tmpname, filename) + os.rename(from_file, to_file) def remove(self, path): os.remove(path) @@ -32,8 +32,10 @@ def remove(self, path): def rmdir(self, directory): os.rmdir(directory) - def join(self, root, path): - return os.path.join(root, path.lstrip('/')) + def join(self, *paths): + if not paths: + return "" + return os.path.join(paths[0], *[path.lstrip("/") for path in paths[1:]]) def dirname(self, path): return os.path.dirname(path) diff --git a/staticgenerator/tests/functional/test_filesystem.py b/staticgenerator/tests/functional/test_filesystem.py new file mode 100644 index 0000000..11d9fcf --- /dev/null +++ b/staticgenerator/tests/functional/test_filesystem.py @@ -0,0 +1,139 @@ +#!/usr/bin/env python +#-*- coding:utf-8 -*- + +import os +from os.path import abspath, join, dirname, exists +import tempfile + +from staticgenerator.staticgenerator import FileSystem + +ROOT_DIR = join(abspath(os.curdir), "test_data") + +def test_can_create_filesystem(): + fs = FileSystem() + + assert fs + assert isinstance(fs, FileSystem) + +def test_current_directory_exists(): + fs = FileSystem() + assert fs.exists(ROOT_DIR) + +def test_directory_not_exists(): + fs = FileSystem() + assert not fs.exists('/fake/dir') + +def test_can_create_directory(): + fs = FileSystem() + + path = join(ROOT_DIR, "test") + + fs.makedirs(path) + + assert exists(path) + + os.rmdir(path) + +def test_can_create_tempfile(): + fs = FileSystem() + + temp_file = fs.tempfile(ROOT_DIR) + + assert len(temp_file) == 2 + + os.remove(temp_file[1]) + +def test_can_write_content_in_tempfile(): + fs = FileSystem() + + temp_file = tempfile.mkstemp() + bytes = fs.write(temp_file[0], "foo") + + assert bytes == 3 + +def test_can_close_tempfile(): + fs = FileSystem() + + temp_file = tempfile.mkstemp() + fs.close(temp_file[0]) + + try: + fs.close(temp_file[0]) + assert False + except OSError: + pass + +def test_can_rename_file(): + fs = FileSystem() + + file_path = join(ROOT_DIR, "some_file") + f = open(file_path, "w") + f.write("content") + f.close() + + new_file_path = join(ROOT_DIR, "new_file") + fs.rename(file_path, new_file_path) + + f = open(new_file_path, "r") + assert f.read() == "content" + + f.close() + +def test_can_remove_file(): + fs = FileSystem() + + file_path = join(ROOT_DIR, "some_file") + f = open(file_path, "w") + f.write("content") + f.close() + + fs.remove(file_path) + + assert not exists(file_path) + +def test_can_remove_dir(): + fs = FileSystem() + + dir_path = join(ROOT_DIR, "some_dir") + os.mkdir(dir_path) + + fs.rmdir(dir_path) + + assert not exists(dir_path) + +def test_join_many_paths(): + fs = FileSystem() + assert fs.join("/fake", "/dir", "/other") == "/fake/dir/other" + +def test_join_single_path(): + fs = FileSystem() + assert fs.join("/fake") == "/fake" + +def test_join_two_paths(): + fs = FileSystem() + assert fs.join("/fake", "/dir") == "/fake/dir" + +def test_join_two_paths_when_second_is_not_rooted(): + fs = FileSystem() + assert fs.join("/fake", "dir") == "/fake/dir" + +def test_join_two_paths_when_second_is_virtual(): + fs = FileSystem() + assert fs.join("/fake", "../dir") == "/fake/../dir" + +def test_join_returns_empty_string_when_empty(): + fs = FileSystem() + assert fs.join('') == '' + +def test_join_returns_empty_string_when_null(): + fs = FileSystem() + assert fs.join() == '' + +def test_join_returns_rooted_path_when_second_path_is_empty(): + fs = FileSystem() + assert fs.join("/root","") == '/root/' + +def test_dirname_returns_last_dir(): + fs = FileSystem() + assert fs.dirname("/root/test/index.html") == '/root/test' +