Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 846a2ef

Browse files
committed
Fixed issues with missing privileges for running ut_run.sql.
Extracted logic from ut_runner into helper package that runs with authid definer (owner of UTPLSQL). Added missing privileges.
1 parent 517245f commit 846a2ef

9 files changed

Lines changed: 177 additions & 122 deletions

client_source/sqlplus/ut_run.sql

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -103,19 +103,17 @@ end;
103103
spool off
104104
set define &
105105

106-
107106
@@set_run_params.sql.tmp
108107

109-
110108
spool run_in_backgroung.sql.tmp
111109
declare
112110
l_output_type varchar2(256) := ut_runner.get_streamed_output_type_name();
113111
l_run_params ut_runner.t_run_params := ut_runner.get_run_params();
112+
l_color_enabled varchar2(5) := case when l_run_params.color_enabled then 'true' else 'false' end;
114113
procedure p(a_text varchar2) is
115114
begin
116115
dbms_output.put_line(a_text);
117116
end;
118-
l_color_enabled varchar2(5) := case when l_run_params.color_enabled then 'true' else 'false' end;
119117
begin
120118
p( 'set serveroutput on size unlimited format truncated');
121119
p( 'set trimspool on');
@@ -126,11 +124,13 @@ begin
126124
p( ' v_reporter ut_reporter_base;');
127125
p( ' v_reporters_list ut_reporters := ut_reporters();');
128126
p( 'begin');
129-
for i in 1 .. cardinality(l_run_params.call_params) loop
130-
p(' v_reporter := '||l_run_params.call_params(i).ut_reporter_name||'('||l_output_type||'());');
131-
p(' v_reporter.output.output_id := '''||l_run_params.call_params(i).output_id||''';');
132-
p(' v_reporters_list.extend; v_reporters_list(v_reporters_list.last) := v_reporter;');
133-
end loop;
127+
if l_run_params.call_params is not null then
128+
for i in 1 .. l_run_params.call_params.count loop
129+
p(' v_reporter := '||l_run_params.call_params(i).ut_reporter_name||'('||l_output_type||'());');
130+
p(' v_reporter.output.output_id := '''||l_run_params.call_params(i).output_id||''';');
131+
p(' v_reporters_list.extend; v_reporters_list(v_reporters_list.last) := v_reporter;');
132+
end loop;
133+
end if;
134134
p( ' ut_runner.run( ut_varchar2_list('||l_run_params.ut_paths||'), v_reporters_list, a_color_console => '||l_color_enabled||' );');
135135
p( 'end;');
136136
p( '/');
@@ -152,13 +152,15 @@ declare
152152
begin
153153
p('declare l_date date := sysdate; begin loop exit when l_date < sysdate; end loop; end;');
154154
p('/');
155-
for i in 1 .. cardinality(l_run_params.call_params) loop
156-
p('set termout '||l_run_params.call_params(i).output_to_screen);
157-
l_need_spool := (l_run_params.call_params(i).output_file_name is not null);
158-
p(case when l_need_spool then 'spool '||l_run_params.call_params(i).output_file_name||chr(10) end||
159-
'select * from table( '||l_output_type||'().get_lines('''||l_run_params.call_params(i).output_id||''') );'||
160-
case when l_need_spool then chr(10)||'spool off' end);
161-
end loop;
155+
if l_run_params.call_params is not null then
156+
for i in 1 .. l_run_params.call_params.count loop
157+
p('set termout '||l_run_params.call_params(i).output_to_screen);
158+
l_need_spool := (l_run_params.call_params(i).output_file_name is not null);
159+
p(case when l_need_spool then 'spool '||l_run_params.call_params(i).output_file_name||chr(10) end||
160+
'select * from table( '||l_output_type||'().get_lines('''||l_run_params.call_params(i).output_id||''') );'||
161+
case when l_need_spool then chr(10)||'spool off' end);
162+
end loop;
163+
end if;
162164
end;
163165
/
164166

source/api/ut_runner.pkb

Lines changed: 3 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
create or replace package body ut_runner is
22

3-
g_run_params t_run_params;
4-
53
procedure run(a_paths ut_varchar2_list, a_reporters ut_reporters, a_color_console boolean := false) is
64
l_items_to_run ut_run;
75
l_listener ut_event_listener;
@@ -46,104 +44,19 @@ create or replace package body ut_runner is
4644
run(ut_varchar2_list(coalesce(a_path, sys_context('userenv', 'current_schema'))), a_reporters, a_color_console);
4745
end run;
4846

49-
function parse_reporting_params(a_params ut_varchar2_list) return tt_call_params is
50-
l_default_call_param t_call_param;
51-
l_call_params tt_call_params := tt_call_params();
52-
l_force_out_to_screen boolean;
53-
begin
54-
for param in(
55-
with
56-
param_vals as(
57-
select regexp_substr(column_value,'-([fos])\=?(.*)',1,1,'c',1) param_type,
58-
regexp_substr(column_value,'-([fos])\=(.*)',1,1,'c',2) param_value
59-
from table(a_params)
60-
where column_value is not null)
61-
select param_type, param_value
62-
from param_vals
63-
where param_type is not null
64-
) loop
65-
if param.param_type = 'f' or l_call_params.last is null then
66-
l_call_params.extend;
67-
l_call_params(l_call_params.last) := l_default_call_param;
68-
if param.param_type = 'f' then
69-
l_call_params(l_call_params.last).ut_reporter_name := param.param_value;
70-
end if;
71-
l_force_out_to_screen := false;
72-
end if;
73-
if param.param_type = 'o' then
74-
l_call_params(l_call_params.last).output_file_name := param.param_value;
75-
if not l_force_out_to_screen then
76-
l_call_params(l_call_params.last).output_to_screen := 'off';
77-
end if;
78-
elsif param.param_type = 's' then
79-
l_call_params(l_call_params.last).output_to_screen := 'on';
80-
l_force_out_to_screen := true;
81-
end if;
82-
end loop;
83-
if l_call_params.count = 0 then
84-
l_call_params.extend;
85-
l_call_params(1) := l_default_call_param;
86-
end if;
87-
return l_call_params;
88-
end;
89-
90-
function parse_paths_param(a_params ut_varchar2_list) return varchar2 is
91-
l_paths varchar2(4000);
92-
begin
93-
begin
94-
select ''''||replace(ut_paths,',',''',''')||''''
95-
into l_paths
96-
from (select regexp_substr(column_value,'-p\=(.*)',1,1,'c',1) as ut_paths from table(a_params) )
97-
where ut_paths is not null;
98-
exception
99-
when no_data_found then
100-
l_paths := 'user';
101-
when too_many_rows then
102-
raise_application_error(-20000, 'Parameter "-p=ut_path(s)" defined more than once. Only one "-p=ut_path(s)" parameter can be used.');
103-
end;
104-
return l_paths;
105-
end;
106-
107-
procedure setup_reporting_output_ids(a_call_params in out nocopy tt_call_params) is
108-
begin
109-
for i in 1 .. cardinality(a_call_params) loop
110-
execute immediate 'begin :l_output_id := '||get_streamed_output_type_name()||'().generate_output_id(); end;'
111-
using out a_call_params(i).output_id;
112-
end loop;
113-
end;
114-
115-
function is_color_enabled(a_params ut_varchar2_list) return boolean is
116-
begin
117-
for i in 1 .. cardinality(a_params) loop
118-
if a_params(i) = '-c' then
119-
return true;
120-
end if;
121-
end loop;
122-
return false;
123-
end;
124-
12547
procedure set_run_params(a_params ut_varchar2_list) is
126-
l_call_params tt_call_params := tt_call_params();
12748
begin
128-
l_call_params := parse_reporting_params(a_params);
129-
g_run_params.ut_paths := parse_paths_param(a_params);
130-
g_run_params.color_enabled := is_color_enabled(a_params);
131-
setup_reporting_output_ids(l_call_params);
132-
g_run_params.call_params := l_call_params;
49+
ut_runner_helper.set_run_params(a_params);
13350
end set_run_params;
13451

13552
function get_run_params return t_run_params is
13653
begin
137-
return g_run_params;
54+
return ut_runner_helper.get_run_params();
13855
end;
13956

14057
function get_streamed_output_type_name return varchar2 is
141-
l_result varchar2(255);
14258
begin
143-
select type_name
144-
into l_result
145-
from user_types where supertype_name = 'UT_OUTPUT_STREAM';
146-
return lower(l_result);
59+
return ut_runner_helper.get_streamed_output_type_name();
14760
end;
14861

14962
end ut_runner;

source/api/ut_runner.pks

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
11
create or replace package ut_runner authid current_user is
22

3-
type t_call_param is record (
4-
ut_reporter_name varchar2(4000) := 'ut_documentation_reporter',
5-
output_file_name varchar2(4000),
6-
output_to_screen varchar2(3) := 'on',
7-
output_id varchar2(4000)
8-
);
9-
10-
type tt_call_params is table of t_call_param;
11-
12-
type t_run_params is record(
13-
ut_paths varchar2(4000),
14-
color_enabled boolean,
15-
call_params tt_call_params
16-
);
3+
subtype t_call_param is ut_runner_helper.t_call_param;
4+
5+
subtype tt_call_params is ut_runner_helper.tt_call_params;
6+
7+
subtype t_run_params is ut_runner_helper.t_run_params;
178

189
/**
1910
* Run suites/tests by path

source/core/ut_runner_helper.pkb

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
create or replace package body ut_runner_helper is
2+
3+
g_run_params t_run_params;
4+
5+
function parse_reporting_params(a_params ut_varchar2_list) return tt_call_params is
6+
l_default_call_param t_call_param;
7+
l_call_params tt_call_params := tt_call_params();
8+
l_force_out_to_screen boolean;
9+
begin
10+
for param in(
11+
with
12+
param_vals as(
13+
select regexp_substr(column_value,'-([fos])\=?(.*)',1,1,'c',1) param_type,
14+
regexp_substr(column_value,'-([fos])\=(.*)',1,1,'c',2) param_value
15+
from table(a_params)
16+
where column_value is not null)
17+
select param_type, param_value
18+
from param_vals
19+
where param_type is not null
20+
) loop
21+
if param.param_type = 'f' or l_call_params.last is null then
22+
l_call_params.extend;
23+
l_call_params(l_call_params.last) := l_default_call_param;
24+
if param.param_type = 'f' then
25+
l_call_params(l_call_params.last).ut_reporter_name := param.param_value;
26+
end if;
27+
l_force_out_to_screen := false;
28+
end if;
29+
if param.param_type = 'o' then
30+
l_call_params(l_call_params.last).output_file_name := param.param_value;
31+
if not l_force_out_to_screen then
32+
l_call_params(l_call_params.last).output_to_screen := 'off';
33+
end if;
34+
elsif param.param_type = 's' then
35+
l_call_params(l_call_params.last).output_to_screen := 'on';
36+
l_force_out_to_screen := true;
37+
end if;
38+
end loop;
39+
if l_call_params.count = 0 then
40+
l_call_params.extend;
41+
l_call_params(1) := l_default_call_param;
42+
end if;
43+
return l_call_params;
44+
end;
45+
46+
function parse_paths_param(a_params ut_varchar2_list) return varchar2 is
47+
l_paths varchar2(4000);
48+
begin
49+
begin
50+
select ''''||replace(ut_paths,',',''',''')||''''
51+
into l_paths
52+
from (select regexp_substr(column_value,'-p\=(.*)',1,1,'c',1) as ut_paths from table(a_params) )
53+
where ut_paths is not null;
54+
exception
55+
when no_data_found then
56+
l_paths := 'user';
57+
when too_many_rows then
58+
raise_application_error(-20000, 'Parameter "-p=ut_path(s)" defined more than once. Only one "-p=ut_path(s)" parameter can be used.');
59+
end;
60+
return l_paths;
61+
end;
62+
63+
procedure setup_reporting_output_ids(a_call_params in out nocopy tt_call_params) is
64+
begin
65+
for i in 1 .. cardinality(a_call_params) loop
66+
execute immediate 'begin :l_output_id := '||get_streamed_output_type_name()||'().generate_output_id(); end;'
67+
using out a_call_params(i).output_id;
68+
end loop;
69+
end;
70+
71+
function is_color_enabled(a_params ut_varchar2_list) return boolean is
72+
begin
73+
for i in 1 .. cardinality(a_params) loop
74+
if a_params(i) = '-c' then
75+
return true;
76+
end if;
77+
end loop;
78+
return false;
79+
end;
80+
81+
procedure set_run_params(a_params ut_varchar2_list) is
82+
l_call_params tt_call_params := tt_call_params();
83+
begin
84+
l_call_params := parse_reporting_params(a_params);
85+
g_run_params.ut_paths := parse_paths_param(a_params);
86+
g_run_params.color_enabled := is_color_enabled(a_params);
87+
setup_reporting_output_ids(l_call_params);
88+
g_run_params.call_params := l_call_params;
89+
end set_run_params;
90+
91+
function get_run_params return t_run_params is
92+
begin
93+
return g_run_params;
94+
end;
95+
96+
function get_streamed_output_type_name return varchar2 is
97+
l_result varchar2(255);
98+
begin
99+
select type_name
100+
into l_result
101+
from user_types where supertype_name = 'UT_OUTPUT_STREAM';
102+
return lower(l_result);
103+
end;
104+
105+
end ut_runner_helper;
106+
/

source/core/ut_runner_helper.pks

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
create or replace package ut_runner_helper authid definer is
2+
3+
type t_call_param is record (
4+
ut_reporter_name varchar2(4000) := 'ut_documentation_reporter',
5+
output_file_name varchar2(4000),
6+
output_to_screen varchar2(3) := 'on',
7+
output_id varchar2(4000)
8+
);
9+
10+
type tt_call_params is table of t_call_param;
11+
12+
type t_run_params is record(
13+
ut_paths varchar2(4000),
14+
color_enabled boolean,
15+
call_params tt_call_params
16+
);
17+
18+
----------------------------
19+
-- Client-side executor helper procedures and functions.
20+
21+
procedure set_run_params(a_params ut_varchar2_list);
22+
23+
function get_run_params return t_run_params;
24+
25+
function get_streamed_output_type_name return varchar2;
26+
27+
end ut_runner_helper;
28+
/

source/create_synonyms_and_grants_for_public.sql

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
Create all necessary grant for the user who owns test packages and want to execute utPLSQL framework
33
*/
44

5-
prompt Granting user
65
set echo off
7-
set feedback off
6+
set feedback on
87
set heading off
98
set verify off
109

@@ -30,6 +29,9 @@ grant execute on ut_runner to public;
3029
grant execute on ut_teamcity_reporter to public;
3130
grant execute on ut_documentation_reporter to public;
3231
grant execute on ut_reporters to public;
32+
grant execute on ut_varchar2_list to public;
33+
grant execute on ut_reporter_base to public;
34+
grant execute on ut_output_dbms_pipe to public;
3335

3436
prompt Creating synonyms for UTPLSQL objects in &&ut3_owner schema to PUBLIC
3537

@@ -50,3 +52,6 @@ create public synonym ut_runner for ut_runner;
5052
create public synonym ut_teamcity_reporter for ut_teamcity_reporter;
5153
create public synonym ut_documentation_reporter for ut_documentation_reporter;
5254
create public synonym ut_reporters for ut_reporters;
55+
create public synonym ut_varchar2_list for ut_varchar2_list;
56+
create public synonym ut_reporter_base for ut_reporter_base;
57+
create public synonym ut_output_dbms_pipe for ut_output_dbms_pipe;

source/create_synonyms_and_grants_for_user.sql

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Create all necessary grant for the user who owns test packages and want to execu
33
*/
44

55
set echo off
6-
set feedback off
6+
set feedback on
77
set heading off
88
set verify off
99

@@ -30,6 +30,9 @@ grant execute on ut_runner to &ut3_user;
3030
grant execute on ut_teamcity_reporter to &ut3_user;
3131
grant execute on ut_documentation_reporter to &ut3_user;
3232
grant execute on ut_reporters to &ut3_user;
33+
grant execute on ut_varchar2_list to &ut3_user;
34+
grant execute on ut_reporter_base to &ut3_user;
35+
grant execute on ut_output_dbms_pipe to &ut3_user;
3336

3437
prompt Creating synonyms for UTPLSQL objects in &&ut3_owner schema to user &&ut3_user
3538

@@ -50,3 +53,6 @@ create or replace synonym &ut3_user .ut_runner for ut_runner;
5053
create or replace synonym &ut3_user .ut_teamcity_reporter for ut_teamcity_reporter;
5154
create or replace synonym &ut3_user .ut_documentation_reporter for ut_documentation_reporter;
5255
create or replace synonym &ut3_user .ut_reporters for ut_reporters;
56+
create or replace synonym &ut3_user .ut_varchar2_list for ut_varchar2_list;
57+
create or replace synonym &ut3_user .ut_reporter_base for ut_reporter_base;
58+
create or replace synonym &ut3_user .ut_output_dbms_pipe for ut_output_dbms_pipe;

0 commit comments

Comments
 (0)