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_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 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_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'))