You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
4.6 KiB

  1. -module(rebar_namespace_SUITE).
  2. -compile(export_all).
  3. -include_lib("common_test/include/ct.hrl").
  4. -include_lib("eunit/include/eunit.hrl").
  5. all() -> [implicit_compile, default_compile, do_compile,
  6. as_default_compile, as_do_compile,
  7. notfound, do_notfound, default_notfound, ns_notfound, ns_found,
  8. as_ns_invalid,
  9. do_ns_chain, do_ns_chain2, do_ns_noarg, do_ns_badcmd].
  10. init_per_testcase(Case, Config0) ->
  11. Config = rebar_test_utils:init_rebar_state(Config0),
  12. AppDir = ?config(apps, Config),
  13. Name = rebar_test_utils:create_random_name("app1_"++atom_to_list(Case)),
  14. Vsn = rebar_test_utils:create_random_vsn(),
  15. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  16. [{name, Name} | Config].
  17. end_per_testcase(_, Config) ->
  18. Config.
  19. implicit_compile(Config) ->
  20. Name = ?config(name, Config),
  21. rebar_test_utils:run_and_check(Config, [],
  22. ["compile"],
  23. {ok, [{app, Name}]}).
  24. default_compile(Config) ->
  25. Name = ?config(name, Config),
  26. rebar_test_utils:run_and_check(Config, [],
  27. ["default","compile"],
  28. {ok, [{app, Name}]}).
  29. do_compile(Config) ->
  30. Name = ?config(name, Config),
  31. rebar_test_utils:run_and_check(Config, [],
  32. ["do", "compile"],
  33. {ok, [{app, Name}]}).
  34. as_default_compile(Config) ->
  35. Name = ?config(name, Config),
  36. rebar_test_utils:run_and_check(Config, [],
  37. ["as", "prod", "default", "compile"],
  38. {ok, [{app, Name}]}).
  39. as_do_compile(Config) ->
  40. Name = ?config(name, Config),
  41. rebar_test_utils:run_and_check(Config, [],
  42. ["as", "prod", "do", "compile"],
  43. {ok, [{app, Name}]}).
  44. notfound(Config) ->
  45. Command = ["fakecommand"],
  46. rebar_test_utils:run_and_check(
  47. Config, [], Command,
  48. {error, io_lib:format("Command ~p not found", [fakecommand])}
  49. ).
  50. do_notfound(Config) ->
  51. Command = ["do", "fakecommand"],
  52. rebar_test_utils:run_and_check(
  53. Config, [], Command,
  54. {error, io_lib:format("Command ~p not found", [fakecommand])}
  55. ).
  56. default_notfound(Config) ->
  57. Command = ["default", "fakecommand"],
  58. rebar_test_utils:run_and_check(
  59. Config, [], Command,
  60. {error, io_lib:format("Command ~p not found", [fakecommand])}
  61. ).
  62. ns_notfound(Config) ->
  63. Command = ["ns", "fakecommand"],
  64. rebar_test_utils:run_and_check(
  65. add_fake_ns_provider(Config), [], Command,
  66. {error, io_lib:format("Command ~p not found in namespace ~p",
  67. [fakecommand, ns])}
  68. ).
  69. ns_found(Config) ->
  70. Command = ["ns", "fake_provider"],
  71. rebar_test_utils:run_and_check(
  72. add_fake_ns_provider(Config), [], Command,
  73. {ok, []}
  74. ).
  75. as_ns_invalid(Config) ->
  76. %% The as namespace is not valid
  77. Command = ["as", "profile", "as", "task"],
  78. rebar_test_utils:run_and_check(
  79. add_fake_ns_provider(Config), [], Command,
  80. {error, "Namespace 'as' is forbidden"}
  81. ).
  82. do_ns_chain(Config) ->
  83. %% `do` is also able to resolve namespaces on
  84. %% commands not found
  85. Command = ["do", "deps,", "ns", "fake_provider,", "deps"],
  86. rebar_test_utils:run_and_check(
  87. add_fake_ns_provider(Config), [], Command,
  88. {ok, []}
  89. ).
  90. do_ns_chain2(Config) ->
  91. %% `do` is also able to resolve namespaces on
  92. %% commands not found
  93. Command = ["do", "ns", "fake_provider,", "deps,", "ns", "fake_provider"],
  94. rebar_test_utils:run_and_check(
  95. add_fake_ns_provider(Config), [], Command,
  96. {ok, []}
  97. ).
  98. do_ns_noarg(Config) ->
  99. %% `do` is also able to resolve namespaces on
  100. %% commands not found
  101. Command = ["do", "ns"],
  102. rebar_test_utils:run_and_check(
  103. add_fake_ns_provider(Config), [], Command,
  104. {error, io_lib:format("Command ~p not found", [ns])}
  105. ).
  106. do_ns_badcmd(Config) ->
  107. %% `do` is also able to resolve namespaces on
  108. %% commands not found
  109. Command = ["do", "ns", "badcmd"],
  110. rebar_test_utils:run_and_check(
  111. add_fake_ns_provider(Config), [], Command,
  112. {error, io_lib:format("Command ~p not found in namespace ~p", [badcmd, ns])}
  113. ).
  114. %%% Helpers %%%
  115. add_fake_ns_provider(Config) ->
  116. State = ?config(state, Config),
  117. State1 = rebar_state:add_provider(
  118. State,
  119. providers:create(
  120. [{name, fake_provider},
  121. {module, ?MODULE},
  122. {namespace, ns},
  123. {deps, []},
  124. {opts, []}]
  125. )
  126. ),
  127. [{state, State1} | Config].
  128. %% callback for the test suite.
  129. do(State) -> {ok, State}.