Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add File.write and File.write_file #313

Merged
merged 1 commit into from

3 participants

@carlosgaldino

They are just simple wrappers for the Erlang functions with the same name

@travisbot

This pull request passes (merged 338f8bb0 into 2973c34).

lib/file.ex
@@ -391,6 +391,37 @@ defmodule File do
end
end
+ @doc """
+ Tries to write `content` to the file referenced by `io`.
+ Returns `:ok` if successful, or `{:error, reason}` if an error occurs.
+
+ Typical error reasons are:
+
+ * :ebadf - The file is not opened for writing.
+ * :enospc - There is a no space left on the device.
+ """
+ def write(io, content) do
@josevalim Owner

Do we really need File.write? Ideally, we would simply use IO.write() for that (or IO.putb).

Ok. I'll remove that.

@josevalim Owner

Then we can rename File.write_file to File.write. :)

Updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@travisbot

This pull request passes (merged 3352b07 into c926b7f).

@josevalim josevalim merged commit 1fb409f into elixir-lang:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 31, 2012
  1. @carlosgaldino

    Add File.write

    carlosgaldino authored
This page is out of date. Refresh to see the latest.
Showing with 49 additions and 0 deletions.
  1. +18 −0 lib/file.ex
  2. +31 −0 test/elixir/file_test.exs
View
18 lib/file.ex
@@ -390,6 +390,24 @@ defmodule File do
end
end
+ @doc """
+ Tries to write `content` to the file `filename`. The file is created if it
+ does not exist. If it exists, the previous contents are overwritten.
+ Returns `:ok` if successful, or `{:error, reason}` if an error occurs.
+
+ Typical error reasons are:
+
+ * :enoent - A component of the file name does not exist.
+ * :enotdir - A component of the file name is not a directory.
+ On some platforms, enoent is returned instead.
+ * :enospc - There is a no space left on the device.
+ * :eacces - Missing permission for writing the file or searching one of the parent directories.
+ * :eisdir - The named file is a directory.
+ """
+ def write(filename, content, modes // []) do
+ F.write_file(filename, content, modes)
+ end
+
## Helpers
# Normalize the given path by removing "..".
View
31 test/elixir/file_test.exs
@@ -248,4 +248,35 @@ defmodule FileTest do
assert File.mkdir('test/elixir/file_test.exs/test/foo') == { :error, :enotdir }
assert !File.exists?('test/elixir/file_test.exs/test/foo')
end
+
+ test :write_normal_content do
+ try do
+ refute File.exists?('test/tmp/tmp_test.txt')
+ assert File.write('test/tmp/tmp_test.txt', 'test text') == :ok
+ assert { :ok, "test text" } == File.read('test/tmp/tmp_test.txt')
+ after
+ :os.cmd('rm test/tmp/tmp_test.txt')
+ end
+ end
+
+ test :write_utf8 do
+ try do
+ refute File.exists?('test/tmp/tmp_test.txt')
+ assert File.write('test/tmp/tmp_test.txt', "Русский\n\n") == :ok
+ assert { :ok, "Русский\n\n" } == File.read('test/tmp/tmp_test.txt')
+ after
+ :os.cmd('rm test/tmp/tmp_test.txt')
+ end
+ end
+
+ test :write_with_options do
+ try do
+ refute File.exists?('test/tmp/tmp_test.txt')
+ assert File.write('test/tmp/tmp_test.txt', "Русский\n\n") == :ok
+ assert File.write('test/tmp/tmp_test.txt', "test text", [:append]) == :ok
+ assert { :ok, "Русский\n\ntest text" } == File.read('test/tmp/tmp_test.txt')
+ after
+ :os.cmd('rm test/tmp/tmp_test.txt')
+ end
+ end
end
Something went wrong with that request. Please try again.