Kaynağa Gözat

[eval] absolutePath fixes (#7856)

* [eval] fix FileSystem.absolutePath

closes #7850

* [lua] attempt to fix FileSystem.absolutePath

see #7850
Simon Krajewski 6 yıl önce
ebeveyn
işleme
1d438bed7d

+ 0 - 5
src/macro/eval/evalStdLib.ml

@@ -1131,10 +1131,6 @@ module StdFileSystem = struct
 		if String.length s > 1 && String.length s <= 3 && s.[1] = ':' then Path.add_trailing_slash s
 		else remove_trailing_slash s
 
-	let absolutePath = vfun1 (fun relPath ->
-		create_unknown (Path.unique_full_path (decode_string relPath))
-	)
-
 	let createDirectory = vfun1 (fun path ->
 		(try Path.mkdir_from_path (Path.add_trailing_slash (decode_string path)) with Unix.Unix_error (_,cmd,msg) -> exc_string (cmd ^ " " ^ msg));
 		vnull
@@ -3187,7 +3183,6 @@ let init_standard_library builtins =
 		"i64ToDouble",StdFPHelper.i64ToDouble;
 	] [];
 	init_fields builtins (["sys"],"FileSystem") [
-		"absolutePath",StdFileSystem.absolutePath;
 		"createDirectory",StdFileSystem.createDirectory;
 		"deleteFile",StdFileSystem.deleteFile;
 		"deleteDirectory",StdFileSystem.deleteDirectory;

+ 25 - 12
std/eval/_std/sys/FileSystem.hx

@@ -19,21 +19,34 @@
  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  * DEALINGS IN THE SOFTWARE.
  */
+
 package sys;
 
 // This class is here so it re-shadows other FileSystem classes in macros,
 // e.g. from hxnodejs.
-
 @:coreApi
-extern class FileSystem {
-	static function exists(path:String):Bool;
-	static function rename(path:String, newPath:String):Void;
-	static function stat(path:String):FileStat;
-	static function fullPath(relPath:String):String;
-	static function absolutePath(relPath:String):String;
-	static function isDirectory(path:String):Bool;
-	static function createDirectory(path:String):Void;
-	static function deleteFile(path:String):Void;
-	static function deleteDirectory(path:String):Void;
-	static function readDirectory(path:String):Array<String>;
+class FileSystem {
+	extern static public function exists(path:String):Bool;
+
+	extern static public function rename(path:String, newPath:String):Void;
+
+	extern static public function stat(path:String):FileStat;
+
+	extern static public function fullPath(relPath:String):String;
+
+	static public function absolutePath(relPath:String):String {
+		if (haxe.io.Path.isAbsolute(relPath))
+			return relPath;
+		return haxe.io.Path.join([Sys.getCwd(), relPath]);
+	}
+
+	extern static public function isDirectory(path:String):Bool;
+
+	extern static public function createDirectory(path:String):Void;
+
+	extern static public function deleteFile(path:String):Void;
+
+	extern static public function deleteDirectory(path:String):Void;
+
+	extern static public function readDirectory(path:String):Array<String>;
 }

+ 3 - 0
std/lua/_std/sys/FileSystem.hx

@@ -68,6 +68,9 @@ class FileSystem {
 	}
 
 	public inline static function absolutePath( relPath : String ) : String {
+		if (haxe.io.Path.isAbsolute(relPath)) {
+			return relPath;
+		}
 		var pwd = lua.lib.luv.Misc.cwd();
 		if (pwd == null) return relPath;
 		return Path.join([pwd, relPath]);

+ 25 - 0
tests/sys/src/TestFileSystem.hx

@@ -1,5 +1,6 @@
 import sys.FileSystem;
 import utest.Assert;
+using StringTools;
 
 class TestFileSystem extends utest.Test {
 	/**
@@ -121,4 +122,28 @@ class TestFileSystem extends utest.Test {
 		FileSystem.createDirectory(testDir); // shouldn't throw
 		Assert.isTrue(FileSystem.isDirectory(testDir));
 	}
+
+	function testAbsolutePath() {
+		var paths = [
+			{ input: "c:\\nadako",   expected: "c:\\nadako" },
+			{ input: "nadako.js",    expected: haxe.io.Path.join([Sys.getCwd(), "nadako.js"]) },
+			{ input: "./nadako.js",  expected: haxe.io.Path.join([Sys.getCwd(), "/./nadako.js"]) },
+			{ input: "/nadako",      expected: "/nadako" }
+		];
+		for (path in paths) {
+			Assert.equals(normPath(path.expected), normPath(FileSystem.absolutePath(path.input)));
+		}
+	}
+
+	static function normPath(p:String, properCase = false):String {
+		if (Sys.systemName() == "Windows")
+		{
+			// on windows, haxe returns lowercase paths with backslashes, drive letter uppercased
+			p = p.substr(0, 1).toUpperCase() + p.substr(1);
+			p = p.replace("/", "\\");
+			if (!properCase)
+				p = p.toLowerCase();
+		}
+		return p;
+	}
 }