Skip to content

Commit a68b50e

Browse files
authored
Merge pull request #2740 from gonzalobf/dereference-symbolic-links-compilation
Always dereference symbolic links when copying files in ct
2 parents 69a5898 + 769ebe2 commit a68b50e

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed

apps/rebar/src/rebar_prv_compile.erl

+1-1
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ copy(Source, Target) ->
503503
{ok, Files} = rebar_utils:list_dir(Source),
504504
case [filename:join([Source, F]) || F <- Files] of
505505
[] -> ok;
506-
Paths -> rebar_file_utils:cp_r(Paths, Target)
506+
Paths -> rebar_file_utils:cp_r(Paths, Target, [{dereference, true}])
507507
end.
508508

509509
delete_if_symlink(Path) ->

apps/rebar/test/rebar_ct_SUITE.erl

+75-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
single_dir_and_single_suite/1,
2323
suite_at_root/1,
2424
suite_at_app_root/1,
25+
data_in_app_test_folder/1,
2526
data_dir_correct/1,
2627
cmd_label/1,
2728
cmd_config/1,
@@ -63,6 +64,8 @@
6364

6465
-include_lib("eunit/include/eunit.hrl").
6566
-include_lib("common_test/include/ct.hrl").
67+
-include_lib("kernel/include/file.hrl").
68+
6669

6770
all() -> [{group, basic_app},
6871
{group, multi_app},
@@ -100,7 +103,8 @@ groups() -> [{basic_app, [], [basic_app_default_dirs,
100103
all_suite,
101104
single_dir_and_single_suite,
102105
suite_at_root,
103-
suite_at_app_root]},
106+
suite_at_app_root,
107+
data_in_app_test_folder]},
104108
{data_dirs, [], [data_dir_correct]},
105109
{ct_opts, [], [cmd_label,
106110
cmd_config,
@@ -193,6 +197,11 @@ init_per_group(dirs_and_suites, Config) ->
193197
AppDir1 = filename:join([AppDir, "apps", Name1]),
194198
rebar_test_utils:create_app(AppDir1, Name1, Vsn1, [kernel, stdlib]),
195199

200+
ok = file:write_file(
201+
filename:join([AppDir, "some_data_in_root.txt"]),
202+
<<"I'm the content of a file in the root folder">>
203+
),
204+
196205
Suite1 = filename:join([AppDir1, "test", Name1 ++ "_SUITE.erl"]),
197206
ok = filelib:ensure_dir(Suite1),
198207
ok = file:write_file(Suite1, test_suite(Name1)),
@@ -205,6 +214,13 @@ init_per_group(dirs_and_suites, Config) ->
205214
Suite2 = filename:join([AppDir2, "test", Name2 ++ "_SUITE.erl"]),
206215
ok = filelib:ensure_dir(Suite2),
207216
ok = file:write_file(Suite2, test_suite(Name2)),
217+
ok = filelib:ensure_dir(filename:join([AppDir2, "test", Name2 ++ "_SUITE_data", "dummy.txt"])),
218+
ok = file:write_file(filename:join([AppDir2, "test", Name2 ++ "_SUITE_data", "some_data.txt"]), <<"hello">>),
219+
ok = file:make_symlink(
220+
filename:join([AppDir, "some_data_in_root.txt"]),
221+
filename:join([AppDir2, "test", Name2 ++ "_SUITE_data", "symlink_some_data_in_root.txt"])
222+
),
223+
208224

209225
Suite3 = filename:join([AppDir, "test", "extras_SUITE.erl"]),
210226
ok = filelib:ensure_dir(Suite3),
@@ -225,6 +241,10 @@ init_per_group(dirs_and_suites, Config) ->
225241

226242
ok = filelib:ensure_dir(filename:join([AppDir, "apps", Name2, "app_root_SUITE_data", "dummy.txt"])),
227243
ok = file:write_file(filename:join([AppDir, "apps", Name2, "app_root_SUITE_data", "some_data.txt"]), <<>>),
244+
ok = file:make_symlink(
245+
filename:join([AppDir, "some_data_in_root.txt"]),
246+
filename:join([AppDir, "apps", Name2, "app_root_SUITE_data", "symlink_some_data_in_root.txt"])
247+
),
228248

229249
{ok, State} = rebar_test_utils:run_and_check(C, [], ["as", "test", "lock"], return),
230250

@@ -787,6 +807,12 @@ suite_at_app_root(Config) ->
787807
DataFile = filename:join([AppDir, "_build", "test", "lib", Name2, "app_root_SUITE_data", "some_data.txt"]),
788808
true = filelib:is_file(DataFile),
789809

810+
SymFile = filename:join([AppDir, "_build", "test", "lib", Name2, "app_root_SUITE_data", "symlink_some_data_in_root.txt"]),
811+
true = filelib:is_file(SymFile),
812+
% At this point, the symbolic link should have been dereference and it is a regular file
813+
{ok, #file_info{type = regular}} = file:read_link_info(SymFile),
814+
?assertEqual({ok, <<"I'm the content of a file in the root folder">>}, file:read_file(SymFile)),
815+
790816
%% Same test again using relative path to the suite from the project root
791817
{ok,Cwd} = file:get_cwd(),
792818
ok = file:set_cwd(AppDir),
@@ -812,6 +838,54 @@ suite_at_app_root(Config) ->
812838

813839
ok.
814840

841+
data_in_app_test_folder(Config) ->
842+
% the ct data folder in app_name/test is properly copied to destination
843+
AppDir = ?config(apps, Config),
844+
[_Name1, Name2] = ?config(appnames, Config),
845+
State = ?config(s, Config),
846+
847+
LibDirs = rebar_dir:lib_dirs(State),
848+
State1 = rebar_app_discover:do(State, LibDirs),
849+
850+
Providers = rebar_state:providers(State1),
851+
Namespace = rebar_state:namespace(State1),
852+
CommandProvider = providers:get_provider(ct, Providers, Namespace),
853+
GetOptSpec = providers:opts(CommandProvider),
854+
{ok, GetOptResult} = getopt:parse(GetOptSpec,
855+
["--suite=" ++ filename:join([AppDir,
856+
"apps",
857+
Name2,
858+
"test",
859+
Name2 ++ "_SUITE"])]),
860+
State2 = rebar_state:command_parsed_args(State1, GetOptResult),
861+
Tests = rebar_prv_common_test:prepare_tests(State2),
862+
{ok, NewState} = rebar_prv_common_test:compile(State2, Tests),
863+
{ok, T} = Tests,
864+
_Opts = rebar_prv_common_test:translate_paths(NewState, T),
865+
DataFile = filename:join([AppDir,
866+
"_build",
867+
"test",
868+
"lib",
869+
Name2,
870+
"test",
871+
Name2 ++ "_SUITE_data",
872+
"some_data.txt"]),
873+
874+
true = filelib:is_file(DataFile),
875+
?assertEqual({ok, <<"hello">>}, file:read_file(DataFile)),
876+
877+
SymFile = filename:join([AppDir,
878+
"_build",
879+
"test",
880+
"lib",
881+
Name2,
882+
"test",
883+
Name2 ++ "_SUITE_data",
884+
"symlink_some_data_in_root.txt"]),
885+
% At this point, the symbolic link should have been dereference and it is a regular file
886+
{ok, #file_info{type = regular}} = file:read_link_info(SymFile),
887+
?assertEqual({ok, <<"I'm the content of a file in the root folder">>}, file:read_file(SymFile)).
888+
815889
%% this test probably only fails when this suite is run via rebar3 with the --cover flag
816890
data_dir_correct(Config) ->
817891
DataDir = ?config(data_dir, Config),

0 commit comments

Comments
 (0)