def test_collecting_units_recurse(self): test_dir = join(self.test_dir, 'test_app', 'test') ensure_dir(test_dir) with open(join(test_dir, 'level1.erl'), 'w') as test: test.write(''' -module(level1). -include_lib("eunit/include/eunit.hrl"). run_test() -> ?_assert(true).''') subdir = join(test_dir, 'sub') ensure_dir(subdir) with open(join(subdir, 'level2.erl'), 'w') as test: test.write(''' -module(level2). -include_lib("eunit/include/eunit.hrl"). run_test() -> ?_assert(true).''') package = Package.from_path(join(self.test_dir, 'test_app')) compiler = EnotCompiler(package) files = compiler._EnotCompiler__get_all_files(compiler.test_path, 'erl') self.assertEqual({ 'level1': test_dir, 'level2': join(test_dir, 'sub') }, files)
def test_unit_test_with_deps(self): app_dir = join(self.test_dir, 'test_app') dep_dir = join(app_dir, 'deps') set_deps( app_dir, # root project depends on dep1 [{ 'name': 'dep1', 'url': 'https://github.com/comtihon/dep1', 'tag': '1.0.0' }]) create(dep_dir, {'<name>': 'dep1'}) # dep1 has dep_api:test/0 with open(join(dep_dir, 'dep1', 'src', 'dep_api.erl'), 'w') as test: test.write(''' -module(dep_api). -export([test/0]). test() -> true.''') app_test_dir = join(app_dir, 'test') ensure_dir(app_test_dir) # dep_api:test/0 is used in unit test of root project with open(join(app_test_dir, 'common.erl'), 'w') as test: test.write(''' -module(common). -include_lib("eunit/include/eunit.hrl"). run_test() -> ?assertEqual(true, dep_api:test()).''') # Compile dep. I only do it in test, as in real life deps will be compiled and linked before running unit. dep = Package.from_path(join(dep_dir, 'dep1')) self.assertEqual(True, EnotCompiler(dep).compile()) package = Package.from_path(app_dir) compiler = EnotCompiler(package) self.assertEqual(True, compiler.unit())
def test_unit_test_fail(self): test_dir = join(self.test_dir, 'test_app', 'test') ensure_dir(test_dir) with open(join(test_dir, 'simple.erl'), 'w') as test: test.write(''' -module(simple). -include_lib("eunit/include/eunit.hrl"). run_test() -> ?assertEqual(true, false).''') package = Package.from_path(join(self.test_dir, 'test_app')) compiler = EnotCompiler(package) self.assertEqual(False, compiler.unit())
def test_disable_prebuild(self, mock_conf): mock_conf.return_value = self.conf_file create(self.tmp_dir, {'<name>': 'project'}) project_dir = join(self.tmp_dir, 'project') test_file_path = join(project_dir, 'test_file') set_prebuild(project_dir, [{ 'shell': 'echo "test" > ' + test_file_path }], disable_prebuild=True) self.assertEqual(False, os.path.exists(test_file_path)) package = Package.from_path(project_dir) compiler = EnotCompiler(package) self.assertEqual(True, compiler.compile()) self.assertEqual(False, os.path.exists(test_file_path))
def test_build_parse_transform_first(self, mock_conf): mock_conf.return_value = self.conf_file create(self.tmp_dir, {'<name>': 'project'}) project_dir = join(self.tmp_dir, 'project') project_src = join(project_dir, 'src') with open(join(project_src, 'p_trans.erl'), 'w') as w: w.write(''' -module(p_trans). -export([parse_transform/2]). -record(support, {add_fun = true, export = true}). parse_transform(AST, _Options) -> do_parse([], AST, #support{}). do_parse(AST, [], _) -> lists:reverse(AST); do_parse(AST, [F = {function, N, _, _, _} | Others], Support = #support{add_fun = true}) -> M = N - 1, AddedFun = {function, M, sum, 2, [{clause, M, [{var, M, 'A'}, {var, M, 'B'}], [], [{op, M, '+', {var, M, 'A'}, {var, M, 'B'}}]}]}, TurnedOff = Support#support{add_fun = false}, do_parse([F | [AddedFun | AST]], Others, TurnedOff); do_parse(AST, [E = {attribute, N, export, _} | Others], Support = #support{export = true}) -> Export = [E | AST], Exported = {attribute, N + 1, export, [{sum, 2}]}, TurnedOff = Support#support{export = false}, do_parse([Exported | Export], Others, TurnedOff); do_parse(AST, [H | Others], Support) -> do_parse([H | AST], Others, Support). ''') with open(join(project_src, 'a_module.erl'), 'w') as w: w.write(''' -module(a_module). -compile([{parse_transform, p_trans}]). -export([hello/0]). hello() -> hello. ''') package = Package.from_path(project_dir) compiler = EnotCompiler(package) self.assertEqual(True, compiler.compile()) self.assertEqual(True, os.path.exists(join(project_dir, 'ebin')))
def test_proper_project_compilation(self): create(self.test_dir, {'<name>': 'proper'}) project_dir = join(self.test_dir, 'proper') ensure_dir(join(project_dir, 'c_src')) with open(join(project_dir, 'c_src/proper.c'), 'w') as w: w.write(''' #include "erl_nif.h" static ERL_NIF_TERM hello(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { return enif_make_string(env, "Hello world!", ERL_NIF_LATIN1); } static ErlNifFunc nif_funcs[] = { {"hello", 0, hello} }; ERL_NIF_INIT(proper,nif_funcs,NULL,NULL,NULL,NULL) ''') with open(join(project_dir, 'src/proper.erl'), 'w') as w: w.write(''' -module(proper). -export([init/0, hello/0]). init() -> erlang:load_nif("priv/proper", 0), io:format("~p~n", [hello()]). hello() -> "NIF library not loaded". ''') package = Package.from_path(project_dir) compiler = EnotCompiler(package) self.assertEqual(True, compiler.compile()) self.assertEqual(True, os.path.exists(join(project_dir, 'priv/proper.so'))) self.assertEqual(True, os.path.exists(join(project_dir, 'ebin/proper.beam'))) p = subprocess.Popen([ 'erl', '-pa', 'ebin', '-run', 'proper', 'init', '-run', 'init', 'stop', '-noshell' ], stdout=PIPE, cwd=project_dir) self.assertEqual(0, p.wait(5000)) self.assertEqual('"Hello world!"\n', p.stdout.read().decode('utf8'))
def test_error_compilation(self, mock_compiler): mock_compiler.return_value = True ensure_dir(self.src_dir) with open(join(self.src_dir, 'improper.erl'), 'w') as w: w.write(''' -module(proper). -export([test/0]). test() -> syntax error here. do_smth(A) -> A + 1. ''') config = EnotConfig({'name': 'test'}) package = Package(self.test_dir, config, None) compiler = EnotCompiler(package) self.assertEqual(False, compiler.compile()) self.assertEqual(False, os.path.exists(join(self.ebin_dir, 'improper.beam')))
def test_common_test_fail(self): test_dir = join(self.test_dir, 'test_app', 'test') ensure_dir(test_dir) with open(join(test_dir, 'common_SUITE.erl'), 'w') as test: test.write(''' -module(common_SUITE). -include_lib("common_test/include/ct.hrl"). -export([all/0]). -export([test/1]). all() -> [test]. test(_Config) -> 1 = 2.''') package = Package.from_path(join(self.test_dir, 'test_app')) compiler = EnotCompiler(package) self.assertEqual(False, compiler.common('test/logs'))
def test_write_app_file_from_src(self): ensure_dir(self.src_dir) with open(join(self.src_dir, 'proper.erl'), 'w') as w: w.write(''' -module(proper). -export([test/0]). test() -> do_smth(1). do_smth(A) -> A + 1. ''') with open(join(self.src_dir, 'proper.app.src'), 'w') as w: w.write(''' {application, proper, [ {description, ""}, {vsn, "{{ app.vsn }}"}, {registered, []}, {modules, {{ modules }}}, {applications, {{ app.std_apps + app.apps }}}, {mod, {proper_app, []}}, {env, []} ]}. ''') with open(join(self.test_dir, 'enot_config.json'), 'w') as w: w.write('''{ \"name\":\"proper\", \"app_vsn\":\"1.0.0\", \"deps\": [{\"name\": \"test_dep\", \"url\": \"http://github/comtihon/test_dep\", \"tag\": \"test_vsn\"}] }''') package = Package.from_path(self.test_dir) compiler = EnotCompiler(package) self.assertEqual(True, compiler.compile()) self.assertEqual(True, os.path.exists(self.ebin_dir)) ls = listdir(self.ebin_dir) self.assertEqual(True, 'proper.beam' in ls) self.assertEqual(True, 'proper.app' in ls) self.assertEqual(2, len(ls)) (name, vsn, deps, _) = parse_app_config(self.ebin_dir, '.app') self.assertEqual('proper', name) self.assertEqual('1.0.0', vsn) self.assertEqual(deps, ['kernel', 'stdlib', 'test_dep'])
def select_compiler(compiler: Compiler, define: str, package: Package): if compiler == Compiler.ENOT: return EnotCompiler(package, define) if compiler == Compiler.REBAR: return RebarCompiler(package) # TODO how to determine rebar3? if compiler == Compiler.ERLANG_MK: return ErlangMKCompiler(package) if compiler == Compiler.MAKEFILE: return MakefileCompiler(package) if compiler == Compiler.BOOTSTRAP: return BootstrapCompiler(package) raise RuntimeError('Unknown compiler ' + compiler.value + ' for ' + package.name)
def test_error_project_compilation(self): create(self.test_dir, {'<name>': 'proper'}) project_dir = join(self.test_dir, 'proper') ensure_dir(join(project_dir, 'c_src')) with open(join(project_dir, 'c_src/proper.c'), 'w') as w: w.write(''' #include "erl_nif.h" static ERL_NIF_TERM hello(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { return enif_make_string(env, "Hello world!", ERL_NIF_LATIN1) // error here } static ErlNifFunc nif_funcs[] = { {"hello", 0, hello} }; ERL_NIF_INIT(proper,nif_funcs,NULL,NULL,NULL,NULL) ''') with open(join(project_dir, 'src/proper.erl'), 'w') as w: w.write(''' -module(proper). -export([init/0, hello/0]). init() -> erlang:load_nif("priv/proper", 0), io:format("~p~n", [hello()]). hello() -> "NIF library not loaded". ''') package = Package.from_path(project_dir) compiler = EnotCompiler(package) self.assertEqual(False, compiler.compile()) self.assertEqual(False, os.path.exists(join(project_dir, 'priv/proper.so'))) self.assertEqual(False, os.path.exists(join(project_dir, 'ebin/proper.beam')))
def test_collecting_units(self): test_dir = join(self.test_dir, 'test_app', 'test') ensure_dir(test_dir) with open(join(test_dir, 'first.erl'), 'w') as test: test.write(''' -module(first). -include_lib("eunit/include/eunit.hrl"). run_test() -> ?_assert(true).''') with open(join(test_dir, 'second.erl'), 'w') as test: test.write(''' -module(second). -include_lib("eunit/include/eunit.hrl"). run_test() -> ?_assert(true).''') package = Package.from_path(join(self.test_dir, 'test_app')) compiler = EnotCompiler(package) files = compiler._EnotCompiler__get_all_files(compiler.test_path, 'erl') self.assertEqual({'first': test_dir, 'second': test_dir}, files)
def test_unit_multiple_test_ok(self, ): test_dir = join(self.test_dir, 'test_app', 'test') ensure_dir(test_dir) with open(join(test_dir, 'simple.erl'), 'w') as test: test.write(''' -module(simple). -include_lib("eunit/include/eunit.hrl"). run_test() -> ?_assert(true).''') subdir = join(test_dir, 'sub') ensure_dir(subdir) with open(join(subdir, 'level2.erl'), 'w') as test: test.write(''' -module(level2). -include_lib("eunit/include/eunit.hrl"). run_test() -> ?_assert(true).''') package = Package.from_path(join(self.test_dir, 'test_app')) compiler = EnotCompiler(package) self.assertEqual(True, compiler.unit())
def test_defines_setting(self, mock_compiler): mock_compiler.return_value = True ensure_dir(self.src_dir) with open(join(self.src_dir, 'proper.erl'), 'w') as w: w.write(''' -module(proper). -export([test/0]). test() -> io:format("~p~n", [?TEST_DEFINE]). ''') config = EnotConfig({'name': 'test'}) package = Package(self.test_dir, config, None) compiler = EnotCompiler(package, 'TEST_DEFINE=test') self.assertEqual(True, compiler.compile()) self.assertEqual(True, os.path.exists(join(self.ebin_dir, 'proper.beam'))) p = subprocess.Popen([ 'erl', '-pa', 'ebin', '-run', 'proper', 'test', '-run', 'init', 'stop', '-noshell' ], stdout=subprocess.PIPE, cwd=self.ebin_dir) self.assertEqual(0, p.wait(5000)) self.assertEqual('test\n', p.stdout.read().decode('utf8'))