about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2023-09-09 09:48:23 -0700
committerKartik K. Agaram <vc@akkartik.com>2023-09-09 09:48:23 -0700
commit97427a77e2a0fe4902cac00ea36714b4c964a306 (patch)
tree6931ec9f7f800a7a1fe10af3902efecc9a682205
parent1a4ab6cda514608223a1c0c741d3cfe8c249b273 (diff)
parent28a6bb37cd08125e5c3424c5895adb515be55e36 (diff)
downloadview.love-97427a77e2a0fe4902cac00ea36714b4c964a306.tar.gz
Merge text.love
-rw-r--r--app.lua57
1 files changed, 46 insertions, 11 deletions
diff --git a/app.lua b/app.lua
index 8a7e0b2..55b1b0d 100644
--- a/app.lua
+++ b/app.lua
@@ -273,17 +273,6 @@ end
 -- various Lua and LÖVE helpers, tests will be able to check the results of
 -- file operations inside the App.filesystem table.
 
-function App.open_for_writing(filename)
-  App.filesystem[filename] = ''
-  return {
-    write = function(self, s)
-              App.filesystem[filename] = App.filesystem[filename]..s
-            end,
-    close = function(self)
-            end,
-  }
-end
-
 function App.open_for_reading(filename)
   if App.filesystem[filename] then
     return {
@@ -299,6 +288,26 @@ function App.open_for_reading(filename)
   end
 end
 
+function App.read_file(filename)
+  return App.filesystem[filename]
+end
+
+function App.open_for_writing(filename)
+  App.filesystem[filename] = ''
+  return {
+    write = function(self, s)
+              App.filesystem[filename] = App.filesystem[filename]..s
+            end,
+    close = function(self)
+            end,
+  }
+end
+
+function App.write_file(filename, contents)
+  App.filesystem[filename] = contents
+  return --[[status]] true
+end
+
 function App.mkdir(dirname)
   -- nothing in test mode
 end
@@ -435,6 +444,19 @@ function App.disable_tests()
           return ok, err
         end
       end
+  App.read_file =
+      function(path)
+        if not is_absolute_path(path) then
+          return --[[status]] false, 'Please use an unambiguous absolute path.'
+        end
+        local f, err = App.open_for_reading(path)
+        if err then
+          return --[[status]] false, err
+        end
+        local contents = f:read()
+        f:close()
+        return contents
+      end
   App.open_for_writing =
       function(filename)
         local result = nativefs.newFile(filename)
@@ -445,6 +467,19 @@ function App.disable_tests()
           return ok, err
         end
       end
+  App.write_file =
+      function(path, contents)
+        if not is_absolute_path(path) then
+          return --[[status]] false, 'Please use an unambiguous absolute path.'
+        end
+        local f, err = App.open_for_writing(path)
+        if err then
+          return --[[status]] false, err
+        end
+        f:write(contents)
+        f:close()
+        return --[[status]] true
+      end
   App.files = nativefs.getDirectoryItems
   App.mkdir = nativefs.createDirectory
   App.remove = nativefs.remove