Skip to content
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

Make rebar_file_utils:system_tmpdir/1 take TMPDIR envvar into account on *nix #2597

Merged
merged 2 commits into from
Jul 13, 2021

Conversation

jaen
Copy link
Contributor

@jaen jaen commented Jul 13, 2021

See the linked issue issue for more details, but the motivation here is that nix, among other things it does, runs package builds as separate users with different temporary directories. But since for rebar3 the temporary file directory is always /tmp, it can potentially result in tests failing with permission issues for files created previously with a different user, that leaked into /tmp.

This PR makes rebar3 honour the TMPDIR environment variable when deciding what the directory for temporary files is on *nix is, avoiding such collisions.

@@ -71,26 +71,30 @@ end_per_testcase(_Test, Config) ->
Config.

raw_tmpdir(_Config) ->
UnixTmpdir = os:getenv("TMPDIR", "/tmp"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that for these tests we could use os:putenv("TMPDIR", "/tmp") first to actually assert that we use the paths properly, then reset it to whatever it was for the follow-up tests, but that's minor and probably not going to be consequential in most cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense, I tried something like this instead then:

with_tmpdir(Tmpdir, TestBody) ->
    PreviousTmpdir = os:getenv("TMPDIR"),
    os:putenv("TMPDIR",  Tmpdir),
    Result = TestBody(),
    os:putenv("TMPDIR", PreviousTmpdir),
    Result.

raw_tmpdir(_Config) ->
    with_tmpdir("/tmp", fun() ->
        case rebar_file_utils:system_tmpdir() of
            "/tmp"  -> ok;
            "./tmp" -> ok
        end
    end).

Is that what you had in mind? Should I use something other than "/tmp" (such as "/test") to make it more explicit it's a test path?

Copy link
Contributor Author

@jaen jaen Jul 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, let me know if the formatting is correct, this is my closest encounter with this syntax since Prolog at the university ;' )

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think that can work. It's indirectly creating a fixture, though, so the most idiomatic way to go in Common test would be something like:

init_per_group(tmpdir, Config) ->
    PreviousTmpDir = os:getenv("TMPDIR"),
    os:putenv("TMPDIR",  TmpDir),
    [{previous_tmp, PreviousTmpDir} | Config];
...

end_per_group(tmpdir, Config) ->
    case ?config(previous_tmp, Config) of
        false -> os:unsetenv("TMPDIR");
        Val -> os:putenv("TMPDIR", Val)
    end,
    Config;
...

Then you won't even need to call with_tmpdir(...) or an equivalent within the test cases. As long as they are within the group they get the right results.

Note that I also made it so if the variable wasn't set, it remains unset after, to avoid the case where TMPDIR="" where suddenly the temporary directory becomes the current working directory.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's certainly much cleaner, thanks for the hint!

In this case TmpDir is undefined though – should I just inline the value or define a constant like -define(TMPDIR, "/test"). and use it both here and in actual test cases?

Copy link
Collaborator

@ferd ferd Jul 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that would make sense. I'd keep -define(TMPDIR, "/tmp") to avoid any inadvertent change but that should all pass fine.

@ferd ferd merged commit 1105538 into erlang:master Jul 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants