Remove references to OSX from fs.coffee and make fs specs pass.

This commit is contained in:
Corey Johnson 2012-02-27 10:17:40 -08:00
parent 3ea7f4d4d3
commit 8cbf4331e4
5 changed files with 82 additions and 40 deletions

View File

@ -10,7 +10,7 @@ NSString *stringFromCefV8Value(const CefRefPtr<CefV8Value>& value) {
NativeHandler::NativeHandler() : CefV8Handler() {
m_object = CefV8Value::CreateObject(NULL);
const char *functionNames[] = {"exists", "read", "absolute", "list", "isFile", "isDirectory", "remove", "open", "terminate"};
const char *functionNames[] = {"exists", "read", "write", " absolute", "list", "isFile", "isDirectory", "remove", "asyncList", "open", "quit"};
NSUInteger arrayLength = sizeof(functionNames) / sizeof(const char *);
for (NSUInteger i = 0; i < arrayLength; i++) {
const char *functionName = functionNames[i];
@ -48,6 +48,23 @@ bool NativeHandler::Execute(const CefString& name,
return true;
}
else if (name == "write") {
NSString *path = stringFromCefV8Value(arguments[0]);
NSString *content = stringFromCefV8Value(arguments[1]);
NSError *error = nil;
BOOL success = [content writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
if (error) {
exception = [[error localizedDescription] UTF8String];
}
else if (!success) {
std::string exception = "Cannot write to '";
exception += [path UTF8String];
exception += "'";
}
}
else if (name == "absolute") {
NSString *path = stringFromCefV8Value(arguments[0]);
@ -96,15 +113,6 @@ bool NativeHandler::Execute(const CefString& name,
return true;
}
else if (name == "isFile") {
NSString *path = stringFromCefV8Value(arguments[0]);
BOOL isDir = false;
BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDir];
retval = CefV8Value::CreateBool(exists && !isDir);
return true;
}
else if (name == "remove") {
NSString *path = stringFromCefV8Value(arguments[0]);
@ -117,6 +125,41 @@ bool NativeHandler::Execute(const CefString& name,
return true;
}
else if (name == "asyncList") {
NSString *path = stringFromCefV8Value(arguments[0]);
bool recursive = arguments[1]->GetBoolValue();
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *relativePaths = [NSArray array];
NSError *error = nil;
if (recursive) {
relativePaths = [fm subpathsOfDirectoryAtPath:path error:&error];
}
else {
relativePaths = [fm contentsOfDirectoryAtPath:path error:&error];
}
if (error) {
exception = [[error localizedDescription] UTF8String];
}
else {
CefRefPtr<CefV8Value> paths = CefV8Value::CreateArray();
for (NSUInteger i = 0; i < relativePaths.count; i++) {
NSString *relativePath = [relativePaths objectAtIndex:i];
NSString *fullPath = [path stringByAppendingPathComponent:relativePath];
paths->SetValue(i, CefV8Value::CreateString([fullPath UTF8String]));
}
CefV8ValueList args;
args.push_back(paths);
CefRefPtr<CefV8Exception> e;
arguments[2]->ExecuteFunction(arguments[2], args, retval, e, true);
exception = e->GetMessage();
}
return true;
}
else if (name == "open") {
NSString *path = stringFromCefV8Value(arguments[0]);
[NSApp open:path];

View File

@ -8,7 +8,7 @@ describe "Project", ->
describe ".getFilePaths()", ->
it "returns a promise which resolves to a list of all file urls in the project, recursively", ->
expectedPaths = (url.replace(project.url, '') for url in fs.list(project.url, true) when fs.isFile url)
expectedPaths = (url.replace(project.url, '') for url in fs.listTree(project.url) when fs.isFile url)
waitsForPromise ->
project.getFilePaths().done (result) ->

View File

@ -1,4 +1,4 @@
fs = require 'fs'
require 'spec-helper'
require path for path in fs.listDirectoryTree($atom.loadPath + "/spec") when /-spec\.coffee$/.test path
require path for path in fs.listTree($atom.loadPath + "/spec") when /-spec\.coffee$/.test path

View File

@ -19,19 +19,19 @@ describe "fs", ->
expect(fs.join('a', 'b/c/', 'd/')).toBe 'a/b/c/d/'
describe ".async", ->
describe ".listFiles(directoryPath, recursive)", ->
directoryPath = null
beforeEach -> directoryPath = require.resolve 'fixtures/dir'
directoryPath = null
beforeEach ->
directoryPath = require.resolve 'fixtures/dir'
describe "when recursive is true", ->
it "returns a promise that resolves to the recursive contents of that directory that are files", ->
waitsForPromise ->
fs.async.listFiles(directoryPath, true).done (result) ->
expect(result).toEqual (path for path in fs.list(directoryPath, true) when fs.isFile(path))
describe ".listTree(directoryPath)", ->
it "returns a promise that resolves to the recursive contents of that directory", ->
waitsForPromise ->
fs.async.listTree(directoryPath).done (result) ->
expect(result).toEqual fs.listTree(directoryPath)
describe "when recursive is false", ->
it "returns a promise that resolves to the contents of that directory that are files", ->
waitsForPromise ->
fs.async.listFiles(directoryPath).done (result) ->
expect(result).toEqual (path for path in fs.list(directoryPath) when fs.isFile(path))
describe ".listFiles(directoryPath)", ->
it "returns a promise that resolves to the contents of that directory", ->
waitsForPromise ->
fs.async.listFiles(directoryPath).done (result) ->
expect(result).toEqual fs.list(directoryPath)

View File

@ -21,10 +21,10 @@ module.exports =
# Return the dirname of the given path. That is the path with any trailing
# non-directory component removed.
directory: (path) ->
if @isDirectory(absPath)
absPath.replace(/\/?$/, '/')
if @isDirectory(path)
path.replace(/\/?$/, '/')
else
absPath.replace(new RegExp("/#{@base(path)}$"), '/')
path.replace(new RegExp("/#{@base(path)}$"), '/')
# Returns true if the file specified by path exists
exists: (path) ->
@ -43,19 +43,14 @@ module.exports =
# Returns true if the file specified by path exists and is a
# regular file.
isFile: (path) ->
$native.isFile path
not $native.isDirectory path
# Returns an array with all the names of files contained
# in the directory path.
list: (path) ->
$native.list(path, false)
# Returns an Array that starts with the given directory, and all the
# directories relative to the given path, discovered by a depth first
# traversal of every directory in any visited directory, not traversing
# symbolic links to directories, in lexically sorted order within
# directories.
listDirectoryTree: (path) ->
listTree: (path) ->
$native.list(path, true)
# Remove a file at the given path. Throws an error if path is not a
@ -69,14 +64,18 @@ module.exports =
# Open, write, flush, and close a file, writing the given content.
write: (path, content) ->
str = OSX.NSString.stringWithUTF8String content
enc = OSX.NSUTF8StringEncoding
str.writeToFile_atomically_encoding_error path, true, enc, null
$native.write(path, content)
async:
listFiles: (path, recursive) ->
listFiles: (path) ->
deferred = $.Deferred()
$atomController.fs.listFilesAtPath_recursive_onComplete path, recursive, (subpaths) ->
$native.asyncList path, false, (subpaths) ->
deferred.resolve subpaths
deferred
listTree: (path) ->
deferred = $.Deferred()
$native.asyncList path, true, (subpaths) ->
deferred.resolve subpaths
deferred