diff options
-rw-r--r-- | src/rebar_compiler_mib.erl | 37 | ||||
-rw-r--r-- | test/rebar_compile_SUITE.erl | 66 |
2 files changed, 91 insertions, 12 deletions
diff --git a/src/rebar_compiler_mib.erl b/src/rebar_compiler_mib.erl index c731c27..499976d 100644 --- a/src/rebar_compiler_mib.erl +++ b/src/rebar_compiler_mib.erl @@ -22,13 +22,44 @@ context(AppInfo) -> out_mappings => Mappings}. needed_files(_, FoundFiles, _, AppInfo) -> - FirstFiles = [], + RebarOpts = rebar_app_info:opts(AppInfo), + MibFirstConf = rebar_opts:get(RebarOpts, mib_first_files, []), + valid_mib_first_conf(MibFirstConf), + Dir = rebar_app_info:dir(AppInfo), + MibFirstFiles = [filename:join(Dir, File) || File <- MibFirstConf], %% Remove first files from found files - RestFiles = [Source || Source <- FoundFiles, not lists:member(Source, FirstFiles)], + RestFiles = [Source || Source <- FoundFiles, not lists:member(Source, MibFirstFiles)], Opts = rebar_opts:get(rebar_app_info:opts(AppInfo), mib_opts, []), - {{FirstFiles, Opts}, {RestFiles, Opts}}. + {{MibFirstFiles, Opts}, {RestFiles, Opts}}. + +valid_mib_first_conf(FileList) -> + Strs = filter_file_list(FileList), + case rebar_utils:is_list_of_strings(Strs) of + true -> true; + false -> ?ABORT("An invalid file list (~p) was provided as part of your mib_first_files directive", + [FileList]) + end. + +filter_file_list(FileList) -> + Atoms = lists:filter( fun(X) -> is_atom(X) end, FileList), + case Atoms of + [] -> + FileList; + _ -> + atoms_in_mib_first_files_warning(Atoms), + lists:filter( fun(X) -> not(is_atom(X)) end, FileList) + end. + +atoms_in_mib_first_files_warning(Atoms) -> + W = "You have provided atoms as file entries in mib_first_files; " + "mib_first_files only expects lists of filenames as strings. " + "The following MIBs (~p) may not work as expected and it is advised " + "that you change these entires to string format " + "(e.g., \"mibs/SOME-MIB.mib\") ", + ?WARN(W, [Atoms]). + dependencies(_, _, _) -> []. diff --git a/test/rebar_compile_SUITE.erl b/test/rebar_compile_SUITE.erl index a300690..ddaad0c 100644 --- a/test/rebar_compile_SUITE.erl +++ b/test/rebar_compile_SUITE.erl @@ -1161,17 +1161,19 @@ umbrella_mib_first_test(Config) -> rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]), - MibsSrc = <<"-- SIMPLE-MIB.\n" + BExporterSrc = <<"-- BEXPORTER-MIB.\n" "-- This is just a simple MIB used for testing!\n" "--\n" -"SIMPLE-MIB DEFINITIONS ::= BEGIN\n" +"BEXPORTER-MIB DEFINITIONS ::= BEGIN\n" "IMPORTS\n" +" TEXTUAL-CONVENTION\n" +" FROM SNMPv2-TC\n" " MODULE-IDENTITY, enterprises\n" " FROM SNMPv2-SMI;\n" "\n" "ericsson MODULE-IDENTITY\n" " LAST-UPDATED\n" -" \"201403060000Z\"\n" +" \"201812050000Z\"\n" " ORGANIZATION\n" " \"rebar\"\n" " CONTACT-INFO\n" @@ -1183,25 +1185,71 @@ umbrella_mib_first_test(Config) -> " \"This very small module is made available\n" " for mib-compilation testing.\"\n" " ::= { enterprises 999 }\n" +"\n" +"Something ::= TEXTUAL-CONVENTION\n" +" STATUS current\n" +" DESCRIPTION \"\"\n" +" SYNTAX OCTET STRING (SIZE (4))\n" +"END\n">>, + + AImporterSrc = <<"-- AIMPORTER-MIB.\n" +"-- This is just a simple MIB used for testing!\n" +"--\n" +"AIMPORTER-MIB DEFINITIONS ::= BEGIN\n" +"IMPORTS\n" +" Something\n" +" FROM BEXPORTER-MIB\n" +" MODULE-IDENTITY, enterprises\n" +" FROM SNMPv2-SMI;\n" +"\n" +"ericsson MODULE-IDENTITY\n" +" LAST-UPDATED\n" +" \"201812050000Z\"\n" +" ORGANIZATION\n" +" \"rebar\"\n" +" CONTACT-INFO\n" +" \"rebar <rebar@example.com>\n" +" or\n" +" whoever is currently responsible for the SIMPLE\n" +" enterprise MIB tree branch (enterprises.999).\"\n" +" DESCRIPTION\n" +" \"This very small module is made available\n" +" for mib-compilation testing.\"\n" +" ::= { enterprises 1000 }\n" "END\n">>, + + ok = filelib:ensure_dir(filename:join([AppDir, "mibs", "dummy"])), - ok = file:write_file(filename:join([AppDir, "mibs", "SIMPLE-MIB.mib"]), MibsSrc), + ok = file:write_file(filename:join([AppDir, "mibs", "AIMPORTER-MIB.mib"]), AImporterSrc), + ok = file:write_file(filename:join([AppDir, "mibs", "BEXPORTER-MIB.mib"]), BExporterSrc), - RebarConfig = [{mib_first_files, ["mibs/SIMPLE-MIB.mib"]}], + FailureRebarConfig = [{mib_first_files, ["mibs/AIMPORTER-MIB.mib"]}], + SuccessRebarConfig = [{mib_first_files, ["mibs/BEXPORTER-MIB.mib"]}], - rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], {ok, [{app, Name}]}), + PrivMibsDir = filename:join([AppsDir, "_build", "default", "lib", Name, "priv", "mibs"]), + + FailureRebarConfig = [{mib_first_files, ["mibs/AIMPORTER-MIB.mib"]}], + catch ( + rebar_test_utils:run_and_check(Config, FailureRebarConfig, ["compile"], {ok, [{app, Name}]}) ), + + %% check that the bin file was NOT cretated + false = filelib:is_file(filename:join([PrivMibsDir, "AIMPORTER-MIB.bin"])), + + + SuccessRebarConfig = [{mib_first_files, ["mibs/BEXPORTER-MIB.mib"]}], + rebar_test_utils:run_and_check(Config, SuccessRebarConfig, ["compile"], {ok, [{app, Name}]}), %% check a bin corresponding to the mib in the mibs dir exists in priv/mibs - PrivMibsDir = filename:join([AppsDir, "_build", "default", "lib", Name, "priv", "mibs"]), - true = filelib:is_file(filename:join([PrivMibsDir, "SIMPLE-MIB.bin"])), + true = filelib:is_file(filename:join([PrivMibsDir, "AIMPORTER-MIB.bin"])), %% check a hrl corresponding to the mib in the mibs dir exists in include - true = filelib:is_file(filename:join([AppDir, "include", "SIMPLE-MIB.hrl"])), + true = filelib:is_file(filename:join([AppDir, "include", "AIMPORTER-MIB.hrl"])), %% check the mibs dir was linked into the _build dir true = filelib:is_dir(filename:join([AppsDir, "_build", "default", "lib", Name, "mibs"])). + only_default_transitive_deps(Config) -> AppDir = ?config(apps, Config), |