# File system mocking

**URL:** <https://forum.crystal-lang.org/t/file-system-mocking/6060>\
**Category:** Help & Support\
**Created:** [October 6, 2023, 6:07pm UTC](https://forum.crystal-lang.org/t/file-system-mocking/6060 "2023-10-06T18:07:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![solvecodes](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/solvecodes/32/2141_2.png) [@solvecodes](https://forum.crystal-lang.org/u/solvecodes)\
**Post date:** [October 6, 2023, 6:07pm UTC](https://forum.crystal-lang.org/t/file-system-mocking/6060/1 "2023-10-06T18:07:00Z")

</div>

is it really possible to use classes and interfaces to mock file system without libraries in crystal ?

---

<div class="post-metadata">

**Author:** ![Devonte](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/devonte/32/1927_2.png) [@Devonte](https://forum.crystal-lang.org/u/Devonte)\
**Post date:** [October 6, 2023, 6:38pm UTC](https://forum.crystal-lang.org/t/file-system-mocking/6060/2 "2023-10-06T18:38:16Z")

</div>

It is possible to monkey-patch `File` and `Dir` for your use-case, but if this is for specs then you might find testing with direct `IO` inputs would be easier then mocking the file system. For example, take the following method:

```crystal
def reverse_contents(file : File) : Nil
  content = file.gets_to_end.reverse
  file.rewind << content
end

```

This method doesn’t need to have access to a file directly, it can instead use `IO` which allows you to do this:

```crystal
require "spec"

it "reverses contents" do
  io = IO::Memory.new
  io << "foo bar baz"
  io.rewind
  reverse_contents io

  io.to_s.should eq "zab rab oof"
end

```

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [October 6, 2023, 10:43pm UTC](https://forum.crystal-lang.org/t/file-system-mocking/6060/3 "2023-10-06T22:43:41Z")

</div>

The short answer is yes. Totally possible for you to do something like:

```crystal
module FileSystemInterface
  abstract def write(path : String | Path, content : String) : Nil
  abstract def read(path : String | Path) : String
end

class LocalFileSystem
  include FileSystemInterface

  def write(path : String | Path, content : String) : Nil
    File.write path, content
  end

  def read(path : String | Path) : String
    File.read path
  end
end

class MockFileSystem
  include FileSystemInterface

  @files = Hash(String, String).new

  def write(path : String | Path, content : String) : Nil
    @files[path.to_s] = content
  end

  def read(path : String | Path) : String
    @files[path.to_s]? || raise "File does not exist"
  end
end

```

Then your logic can depend upon `FileSystemInterface`, defaulting to `LocalFileSystem`, but using `MockFileSystem` in specs.

---

<div class="post-metadata">

**Author:** ![solvecodes](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/solvecodes/32/2141_2.png) [@solvecodes](https://forum.crystal-lang.org/u/solvecodes)\
**Post date:** [October 7, 2023, 5:22pm UTC](https://forum.crystal-lang.org/t/file-system-mocking/6060/4 "2023-10-07T17:22:46Z")

</div>

this works with file extensions ? i want to obfuscate the file extension i want to make pdf looks like txt but actually it is txt is it possible with classes and interfaces ?

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [October 7, 2023, 5:29pm UTC](https://forum.crystal-lang.org/t/file-system-mocking/6060/5 "2023-10-07T17:29:25Z")

</div>

If that’s what you mean by “filesystem mocking” then I’m not sure I follow why you think you need classes and interfaces. If you just want to save a PDF with a `.txt` file extension then you can just do that when you save the file.
