aboutsummaryrefslogtreecommitdiffstats
path: root/lib/asn1/test/test_compile_options.erl
blob: a622d5bfd24f62c37705f187b72375385d29cf4c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2005-2011. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%
%%

-module(test_compile_options).

-include_lib("test_server/include/test_server.hrl").


-export([wrong_path/1,comp/2,path/1,ticket_6143/1,noobj/1,
	 record_name_prefix/1,verbose/1,warnings_as_errors/1]).

%% OTP-5689
wrong_path(Config) ->
    Pid=spawn(?MODULE,comp,[self(),Config]),
    receive
	_Err ->
	    ok
    after 10000 ->
	    exit(Pid,failure),
	    error
    end.

comp(Parent,Config) ->
    DataDir = ?config(data_dir,Config),
    OutDir = ?config(priv_dir,Config),
    %%?line true = code:add_patha(?config(priv_dir,Config)),
    io:format("DataDir: ~p~n",[DataDir]),
    ?line Err=asn1ct:compile(DataDir++"NoImport",[{i,OutDir},{i,filename:join([DataDir,"subdir"])},{outdir,OutDir}]),
    io:format("compiling process terminated with value: ~p~n",[Err]),
    Parent!Err.

%% OTP-5701

path(Config) ->
    DataDir = ?config(data_dir,Config),
    OutDir = ?config(priv_dir,Config),
    {ok,CWD} = file:get_cwd(),
    ?line file:set_cwd(filename:join([DataDir,subdir])),

    %%?line ok=asn1ct:compile(filename:join([DataDir,"../MyMerge.set.asn"]),[{inline,mymerge},{outdir,OutDir}]),
    ?line ok=asn1ct:compile("../MyMerge.set.asn",[{inline,mymerge},{outdir,OutDir}]),

    ?line ok=outfiles_check(OutDir),
    ?line outfiles_remove(OutDir),

    file:set_cwd(filename:join([DataDir,subdir,subsubdir])),
    ?line ok = asn1ct:compile('../../MyMerge.set.asn',[{inline,mymerge},{i,'..'},{outdir,OutDir}]),

    ?line ok=outfiles_check(OutDir,outfiles2()),
    file:set_cwd(CWD),
    ok.

ticket_6143(Config) ->
    DataDir = ?config(data_dir,Config),
    OutDir = ?config(priv_dir,Config),
    io:format("DataDir: ~p~n",[DataDir]),

    ?line ok=asn1ct:compile(filename:join([DataDir,"AA1"]),[{i,DataDir},{outdir,OutDir}]),
    ok.

noobj(Config) ->
    DataDir = ?config(data_dir,Config),
    OutDir = ?config(priv_dir,Config),
    
    code:purge('P-Record'),
    file:delete(filename:join([OutDir,'P-Record.erl'])),
    file:delete(filename:join([OutDir,'P-Record.beam'])),
    ?line ok=asn1ct:compile(filename:join([DataDir,"P-Record"]),
			    [noobj,{outdir,OutDir}]),
%    ?line false = code:is_loaded('P-Record'),
    ?line {ok,_} = file:read_file_info(filename:join([OutDir,
						      "P-Record.erl"])),
    ?line {error,enoent} =
	file:read_file_info(filename:join([OutDir,"P-Record.beam"])),
    ?line {ok,_} = c:c(filename:join([OutDir,'P-Record']),
		       [{i,OutDir},{outdir,OutDir}]),
    ?line {file,_} = code:is_loaded('P-Record'),
    
    code:purge('P-Record'),
    code:delete('P-Record'),
    code:purge('p_record'),
    code:delete('p_record'),
    file:delete(filename:join([OutDir,'P-Record.erl'])),
    file:delete(filename:join([OutDir,'P-Record.beam'])),
    file:delete(filename:join([OutDir,'p_record.erl'])),
    file:delete(filename:join([OutDir,'p_record.beam'])),
    ?line ok=asn1ct:compile(filename:join([DataDir,"p_record.set.asn"]),[asn1config,ber_bin,optimize,noobj,{outdir,OutDir}]),
%%     ?line false = code:is_loaded('P-Record'),
%%     ?line false = code:is_loaded('p_record'),
    ?line {error,enoent} =
	file:read_file_info(filename:join([OutDir,"P-Record.beam"])),
    ?line {error,enoent} =
	file:read_file_info(filename:join([OutDir,"P-Record.erl"])),
    ?line {error,enoent} =
	file:read_file_info(filename:join([OutDir,"p_record.beam"])),
    io:format("read_file_info: p_record.erl~n",[]),
    ?line {ok,_} =
	file:read_file_info(filename:join([OutDir,"p_record.erl"])),
    io:format("c:c: p_record.erl~n",[]),
    ?line {ok,_} = c:c(filename:join([OutDir,'p_record']),
		       [{i,OutDir},{outdir,OutDir}]),
    io:format("code:is_loaded: p_record.erl~n",[]),
    ?line {file,_} = code:is_loaded('p_record'),
    io:format("file:delete: p_record.erl~n",[]),
    file:delete(filename:join([OutDir,'p_record.erl'])),
    file:delete(filename:join([OutDir,'p_record.beam'])).

verbose(Config) when is_list(Config) ->
    DataDir = ?config(data_dir,Config),
    OutDir = ?config(priv_dir,Config),
    Asn1File = filename:join([DataDir,"Comment.asn"]),

    %% Test verbose compile
    ?line test_server:capture_start(),
    ?line ok = asn1ct:compile(Asn1File, [{i,DataDir},{outdir,OutDir},noobj,verbose]),
    ?line test_server:capture_stop(),
    ?line [Line0|_] = test_server:capture_get(),
    ?line true = lists:prefix("Erlang ASN.1 version", Line0),

    %% Test non-verbose compile
    ?line test_server:capture_start(),
    ?line ok = asn1ct:compile(Asn1File, [{i,DataDir},{outdir,OutDir},noobj]),
    ?line test_server:capture_stop(),
    ?line [] = test_server:capture_get(),
    ok.

warnings_as_errors(Config) when is_list(Config) ->
    PrivDir = ?config(priv_dir,Config),
    Asn1File = filename:join([PrivDir,"WERROR.asn1"]),
    OutFile = filename:join([PrivDir,"WERROR.erl"]),
    Opts = [{outdir,PrivDir},noobj,verbose],

    %% Generate WERR.asn to emit warning
    %% Warning: Wrong format of type/value
    %%     false/{'Externalvaluereference',_,'WERR',noInvokeId}
    Warn = <<"WERROR DEFINITIONS IMPLICIT TAGS ::=\n"
	     "\n"
	     "BEGIN\n"
	     "\n"
	     "InvokeId ::= CHOICE\n"
	     "{\n"
	     "    present INTEGER,\n"
	     "    absent NULL\n"
	     "}\n"
	     "\n"
	     "noInvokeId InvokeId ::= absent:NULL\n"
	     "\n"
	     "NoInvokeId InvokeId ::= {noInvokeId}\n"
	     "\n"
	     "END -- end of useful definitions.\n">>,
    ?line ok = file:write_file(Asn1File, Warn),

    %% Test warnings_as_errors compile
    ?line false = filelib:is_regular(OutFile),
    ?line {error, _} = asn1ct:compile(Asn1File, [warnings_as_errors|Opts]),
    ?line false = filelib:is_regular(OutFile),

    %% Test normal compile
    ?line ok = asn1ct:compile(Asn1File, Opts),
    ?line true = filelib:is_regular(OutFile),
    ?line ok = file:delete(OutFile),
    ok.

outfiles_check(OutDir) ->
    outfiles_check(OutDir,outfiles1()).


outfiles_check(_OutDir,[])->
    ok;
outfiles_check(OutDir,[H|T]) ->
    io:format("File: ~p~n",[filename:join([OutDir,H])]),
    ?line {ok,_}=file:read_file_info(filename:join([OutDir,H])),
    outfiles_check(OutDir,T).

outfiles1() ->
    ["mymerge.erl","mymerge.beam","MyMerge.asn1db","MyMerge.beam",
     "MyMerge.erl","MyMerge.hrl"].
outfiles2() ->
    ["MyMerge.beam","mymerge.erl","MyMerge.asn1db","MyMerge.erl",
     "mymerge.beam"].

outfiles_remove(OutDir) ->
    lists:foreach(fun(F)-> file:delete(filename:join([OutDir,F])) end,
		  outfiles1()).

record_name_prefix(Config) ->
    DataDir = ?config(data_dir,Config),
    OutDir = ?config(priv_dir,Config),
    ok = b_SeqIn(DataDir,OutDir),
    ok = a_SeqIn(DataDir,OutDir).

b_SeqIn(DataDir,OutDir) ->
    asn1ct:compile(filename:join([DataDir,'Seq']),
		   [{record_name_prefix,"b_"},{outdir,OutDir}]),
    io:format("FileName: ~p~nOutDir:~p~n",
	      [filename:join([DataDir,'b_SeqIn']),OutDir]),
    ?line {ok,_} = compile:file(filename:join([DataDir,'b_SeqIn']),
			  [{i,OutDir}]),
    ?line 'b_SeqIn' = b_SeqIn:record_name(),
    ok.

a_SeqIn(DataDir,OutDir) -> 
    asn1ct:compile(filename:join([DataDir,'Seq']),
		   [{record_name_prefix,"a_"},{outdir,OutDir}]),
    ?line {ok,_} = compile:file(filename:join([DataDir,'a_SeqIn']),
			  [{i,OutDir}]),
    ?line 'a_SeqIn' = a_SeqIn:record_name(),
    ok.