-
Notifications
You must be signed in to change notification settings - Fork 88
Add :tmp_dir application config setting #145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add :tmp_dir application config setting #145
Conversation
achempion
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for submitting the PR. I can merge it after these changes
- use
:tmp_diroption name - have a test case covering both
TMPDIRand:tmp_diroptions (search fortest "recv_timeout" doin the codebase for a setup example) - update documentation for the new option
|
Thanks. I improved the tests in I am not sure what you mean with testing for both cases for tests in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the update, here are couple of things that I noticed
- the testing approach is too complicated
- documentation can live inside the
defmodule Waffle.Definition.Storagemodule instead of readme
I am not sure what you mean with testing for both cases for tests in store_test.ex
I linked an abstract example that tests a configuration option.
ec8a055 to
d31781f
Compare
|
Thanks.
|
achempion
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looking good, left couple of comments
lib/waffle/definition/storage.ex
Outdated
| tmp_dir: "/path/to/custom/tmp" | ||
|
|
||
| Waffle may fail to remove temporary files if the process using them crashes. | ||
| Having a periodic cleanup process or using a custom temporary directory is recommended. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: can we please remove this line (122)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed
lib/waffle/file.ex
Outdated
| @doc """ | ||
| Directory used for temporary files. | ||
| """ | ||
| def temp_dir() do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: defp tmp_dir()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we keep it public? then I can easily read the exact temp dir from Waffle in my cleanup genserver. (we can read it from the application env, but may be handy to have this public)
lib/waffle/file.ex
Outdated
| Path.join(temp_dir(), file_name) | ||
| end | ||
|
|
||
| @doc """ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can skip on doc here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed
test/actions/store_test.exs
Outdated
|
|
||
| with_mock Waffle.Storage.S3, | ||
| put: fn DummyDefinition, _, {%{file_name: "favicon.ico", path: path}, nil} -> | ||
| String.starts_with?(path, tmp_dir) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will test still pass if key configuration Application.put_env(:waffle, :tmp_dir, tmp_dir) is removed from it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we leave the default env :tmp_dir (nil), then this test fails, because the file will be written to /tmp instead of waffletest/store_tmp
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With which error it fails with? I can't see any asserts here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah no sorry- missed that. added assert here and in the other test where it was neither done
test/actions/store_test.exs
Outdated
| Application.put_env(:waffle, :recv_timeout, 5_000) | ||
| end | ||
|
|
||
| test "custom tmp_dir used for temp files and succeeds if exists" do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we do a simpler approach with having a simple unit test for generate_temporary_path/1 function insdie the file_test.exs file (we would need to create it)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, done!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't find the file_test.exs in the PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
forgot to git add, done now
lib/waffle/file.ex
Outdated
| |> Kernel.<>(string_extension) | ||
|
|
||
| Path.join(System.tmp_dir(), file_name) | ||
| Path.join(temp_dir(), file_name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tmp_dir()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
|
Thanks for the feedback, ready for another round |
test/actions/store_test.exs
Outdated
| Application.put_env(:waffle, :recv_timeout, 5_000) | ||
| end | ||
|
|
||
| test "custom tmp_dir used for temp files and succeeds if exists" do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't find the file_test.exs in the PR
test/actions/store_test.exs
Outdated
|
|
||
| with_mock Waffle.Storage.S3, | ||
| put: fn DummyDefinition, _, {%{file_name: "favicon.ico", path: path}, nil} -> | ||
| String.starts_with?(path, tmp_dir) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With which error it fails with? I can't see any asserts here
test/actions/store_test.exs
Outdated
| File.rm_rf!(tmp_dir) | ||
| end | ||
|
|
||
| test "default tmp_dir used for temp files and succeeds if exists" do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can skp on updating the store_test as file_test should be enough for this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let me know if you want to remove the store tests. I think it doesn't hurt to have them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would like to remove them to reduce test complexity
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, reverted back to master
test/storage/local_test.exs
Outdated
| File.mkdir_p("waffletest/uploads") | ||
| File.mkdir_p("waffletest/tmp") | ||
| System.put_env("TMPDIR", "waffletest/tmp") | ||
| Application.put_env(:waffle, :tmp_dir, "waffletest/tmp") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would like to keep the default tmpdir setting
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, reverted
achempion
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
left some comments, plrease remove the tests from store_test
test/file_test.exs
Outdated
| @@ -0,0 +1,38 @@ | |||
| defmodule WaffleTest.File do | |||
| use ExUnit.Case | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be async: false to not overlap with other tests on global state changes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, default is false from ExUnit.Case's docs. Anyway specified now explicitly.
* `:async` - configures tests in this module to run concurrently with
tests in other modules. Tests in the same module never run concurrently.
It should be enabled only if tests do not change any global state.
Defaults to `false`.
test/file_test.exs
Outdated
| describe "generate_temporary_path/1" do | ||
| test "uses configured tmp_dir" do | ||
| previous = Application.get_env(:waffle, :tmp_dir, :not_set) | ||
| custom_tmp = System.tmp_dir() <> "/waffle_test_custom" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we move this inside the module attribute?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's only used for this test, not in general, so I am not sure it makes sense. I did it anyway.
| use ExUnit.Case | ||
|
|
||
| describe "generate_temporary_path/1" do | ||
| test "uses configured tmp_dir" do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks good, can we add a couple of changes
- move path into the module attribute
- use on_exit callback for the cleanup
- the test flow can be simpler:
- on_exit setup
- put env
- call function and assert on the same line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok done!
achempion
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looking great, left a couple of comments
test/actions/store_test.exs
Outdated
| File.rm_rf!(tmp_dir) | ||
| end | ||
|
|
||
| test "default tmp_dir used for temp files and succeeds if exists" do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would like to remove them to reduce test complexity
test/file_test.exs
Outdated
| def setup do | ||
| File.mkdir_p!(@custom_tmpdir) | ||
|
|
||
| on_exit fn -> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as we need it only inside a single test, it can be written like this
test "uses configured tmp_dir" do
File.mkdir_p!(@custom_tmpdir)
on_exit fn ->
Application.delete_env(:waffle, :tmp_dir)
File.rm_rf(@custom_tmpdir)
end
Application.put_env(:waffle, :tmp_dir, @custom_tmpdir)
assert Waffle.File.generate_temporary_path() |> String.starts_with?(@custom_tmpdir)
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok. not sure it makes sense to keep the module attribute, as it's only used here now- kept for now, as it's what you wanted earlier. No setup function anymore for the module.
test/file_test.exs
Outdated
| defmodule WaffleTest.File do | ||
| use ExUnit.Case, async: false | ||
|
|
||
| @custom_tmpdir System.tmp_dir() <> "/waffle_test_custom" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: @custom_tmp_dir
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
test/file_test.exs
Outdated
| assert Waffle.File.generate_temporary_path() |> String.starts_with?(@custom_tmpdir) | ||
| end | ||
|
|
||
| test "generates path in default tmp_dir when config not set" do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: "uses system tmp_dir"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
lib/waffle/file.ex
Outdated
| Path.join(tmp_dir(), file_name) | ||
| end | ||
|
|
||
| def tmp_dir() do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we please make it private? Any public interface is hard to change once external apps/libraries will start using it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, done.
|
Done now, ready for another round. |
|
@achempion I think it's ready, let me know what you think. thanks! |
achempion
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking great, please squash git commits so I can merge keeping the clean git history
test/file_test.exs
Outdated
| assert Waffle.File.generate_temporary_path() |> String.starts_with?(@custom_tmp_dir) | ||
| on_exit fn -> | ||
| Application.delete_env(:waffle, :tmp_dir) | ||
| File.rm_rf(@custom_tmp_dir) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: File.rm_rf!/1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done!
|
please check the CI build message |
8d28548 to
f58ce94
Compare
|
all good, should be ok now. I don't know how to make CI run, seems to not happen by default on new commits. |
|
@achempion thanks! Is it possible to create an Hex release with these changes? |
|
Thanks for the PR. I'll do a release soon. You can pin the dependency by the git hash in `mix.ex` directly from github in the meantime.
|
Allows to specify where to save temporary files to. See #144 for more details