this repo has no description
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add real SimFile operations

garrison c1f98372 16422c46

+54 -8
+7 -6
lib/trinity/sim_file.ex
··· 16 16 @spec open(String.t, [atom]) :: {:ok, file_descriptor} | {:error, posix} 17 17 def open(path, modes) do 18 18 case get_sim() do 19 - nil -> raise "not supported" 19 + nil -> File.open(path, modes) 20 20 _sim -> sim_open(path, modes) 21 21 end 22 22 end ··· 24 24 @spec pread(file_descriptor, non_neg_integer, non_neg_integer) :: {:ok, binary} | :eof | {:error, posix} 25 25 def pread(fd, loc, bytes) do 26 26 case get_sim() do 27 - nil -> raise "not supported" 27 + nil -> :file.pread(fd, loc, bytes) 28 28 _sim -> sim_pread(fd, loc, bytes) 29 29 end 30 30 end ··· 32 32 @spec pwrite(term, non_neg_integer, binary) :: :ok | {:error, term} 33 33 def pwrite(fd, loc, bytes) do 34 34 case get_sim() do 35 - nil -> raise "not supported" 35 + nil -> :file.pwrite(fd, loc, bytes) 36 36 _sim -> sim_pwrite(fd, loc, bytes) 37 37 end 38 38 end ··· 40 40 @spec mkdir_p(String.t) :: :ok | {:error, term} 41 41 def mkdir_p(path) do 42 42 case get_sim() do 43 - nil -> raise "not supported" 43 + nil -> File.mkdir_p(path) 44 44 _sim -> sim_mkdir_p(path) 45 45 end 46 46 end ··· 48 48 @spec exists?(String.t) :: boolean 49 49 def exists?(path) do 50 50 case get_sim() do 51 - nil -> raise "not supported" 51 + # TODO: pass opts through 52 + nil -> File.exists?(path, [:raw]) 52 53 _sim -> sim_exists?(path) 53 54 end 54 55 end ··· 56 57 @spec ls(String.t) :: {:ok, [String.t]} | {:error, posix} 57 58 def ls(path) do 58 59 case get_sim() do 59 - nil -> raise "not supported" 60 + nil -> File.ls(path) 60 61 _sim -> sim_ls(path) 61 62 end 62 63 end
+1 -1
test/test_helper.exs
··· 1 - ExUnit.start() 1 + ExUnit.start(exclude: [disable: true])
+46 -1
test/trinity_test.exs
··· 1 1 defmodule TrinityTest do 2 2 use ExUnit.Case 3 3 4 - alias Trinity.{Sim, SimProcess, SimPersistentTerm, SimLogger, Scheduler} 4 + alias Trinity.{Sim, SimProcess, SimPersistentTerm, SimFile, SimLogger, Scheduler} 5 5 import Trinity.Scheduler, only: [receive_yield: 1] 6 6 require SimLogger 7 7 ··· 144 144 145 145 require Logger 146 146 Logger.info(message) 147 + end 148 + 149 + describe "fs" do 150 + @describetag :fs 151 + @describetag :disable 152 + @describetag :tmp_dir 153 + 154 + test "read/write", %{tmp_dir: tmp_dir} do 155 + path = Path.join(tmp_dir, "foobar.txt") 156 + {:ok, fd} = SimFile.open(path, [:read, :write, :raw]) 157 + 158 + text = "foo bar baz\n" 159 + :ok = SimFile.pwrite(fd, 100, text) 160 + {:ok, result} = SimFile.pread(fd, 100, byte_size(text)) 161 + assert result == text 162 + 163 + :ok = SimFile.pwrite(fd, 101, "ool") 164 + {:ok, result} = SimFile.pread(fd, 100, byte_size(text)) 165 + assert result == "foolbar baz\n" 166 + end 167 + 168 + test "paths", %{tmp_dir: tmp_dir} do 169 + subdir = Path.join([tmp_dir, "foo", "bar"]) 170 + path = Path.join(subdir, "foobar.txt") 171 + 172 + assert {:ok, []} = SimFile.ls(tmp_dir) 173 + assert {:error, :enoent} = SimFile.ls(subdir) 174 + refute SimFile.exists?(subdir) 175 + refute SimFile.exists?(path) 176 + 177 + assert {:error, :enoent} = SimFile.open(path, [:read, :write, :raw]) 178 + assert :ok = SimFile.mkdir_p(subdir) 179 + 180 + assert {:ok, ["foo"]} = SimFile.ls(tmp_dir) 181 + assert {:ok, []} = SimFile.ls(subdir) 182 + assert SimFile.exists?(subdir) 183 + refute SimFile.exists?(path) 184 + 185 + assert {:ok, _fd} = SimFile.open(path, [:read, :write, :raw]) 186 + 187 + assert {:ok, ["foo"]} = SimFile.ls(tmp_dir) 188 + assert {:ok, ["foobar.txt"]} = SimFile.ls(subdir) 189 + assert SimFile.exists?(subdir) 190 + assert SimFile.exists?(path) 191 + end 147 192 end 148 193 end