Name | Total Lines | Lines of Code | Total Coverage | Code Coverage |
---|---|---|---|---|
library/generic/file_library.rb | 312 | 132 | 73.08%
|
36.36%
|
Code reported as executed by Ruby looks like this...and this: this line is also marked as covered.Lines considered as run by rcov, but not reported by Ruby, look like this,and this: these lines were inferred by rcov (using simple heuristics).Finally, here's a line marked as not executed.
2 =begin |
3 *Name : FileLibrary |
4 *Description : class that define methods for file related manipulations |
5 *Author : Chandra sekaran |
6 *Creation Date : 23/08/2014 |
7 *Updation Date : |
8 =end |
9 |
10 module CUKES |
11 module FileLibrary |
12 include DataMagic |
13 |
14 # Description : checks for file existance and returns the boolean result |
15 # Author : Chandra sekaran |
16 # Arguments : |
17 # str_file_path : absolute path of file |
18 # Return values : |
19 # bool_file : boolean variable that hold the result of the file existence |
20 # |
21 def is_file_exists(str_file_path) |
22 bool_file = File.exists?(str_file_path)#get_absolute_path(str_file_name) ) |
23 bool_file |
24 rescue Exception => ex |
25 $log.error("Error while checking for existence of #{str_file_path} : #{ex}") |
26 exit |
27 end |
28 |
29 # Description : opens the yml file |
30 # Author : Chandra sekaran |
31 # Arguments : |
32 # str_file_path : absolute path of yml file |
33 # Return values : |
34 # obj_file : hash value of the yml file content |
35 # |
36 def open_yml_file(str_file_path) |
37 obj_file = YAML.load_file(File.open(str_file_path)) #get_absolute_path(str_file_name)) # obj_file = YAML.load_file(format_filepath(str_file_name)) |
38 obj_file |
39 rescue Exception => ex |
40 $log.error("Error while opening #{str_file_path} : #{ex}") |
41 exit |
42 end |
43 |
44 # Description : opens the excel file |
45 # Author : Chandra sekaran |
46 # Arguments : |
47 # str_file_name : absolute path of excel file |
48 # Return values : |
49 # obj_file : file object of the workbook |
50 # |
51 def open_excel_file(str_file_name) |
52 Spreadsheet.client_encoding = 'UTF-8' |
53 obj_file = Spreadsheet.open(get_absolute_path(str_file_name)) |
54 $log.success("File #{str_file_name} opened successfully.") |
55 obj_file |
56 rescue Exception => ex |
57 $log.error("Error while opening #{str_file_name} : #{ex}") |
58 exit |
59 end |
60 |
61 # Description : closes the given file object |
62 # Author : Chandra sekaran |
63 # Arguments : |
64 # file_object : object of the file to be closed |
65 # |
66 def close_file(file_object) |
67 file_object.close |
68 #$log.success("File #{file_object} closed successfully.") |
69 rescue Exception => ex |
70 #file_object = nil |
71 $log.error("Error while closing #{file_object} : #{ex}") |
72 exit |
73 end |
74 |
75 # Description : creates a new directory under the given path |
76 # Author : Chandra sekaran |
77 # Arguments : |
78 # str_directory_path : absolute path of directory |
79 # |
80 def create_directory(str_directory_path) |
81 unless File.directory?(str_directory_path) |
82 FileUtils.mkdir_p(str_directory_path) |
83 end |
84 #$log.success("New directory created : #{str_directory_path}") |
85 rescue Exception => ex |
86 $log.error("Error in creating directory #{str_directory_path} : #{ex}") |
87 exit |
88 end |
89 |
90 # Description : deletes an object |
91 # Author : Chandra sekaran |
92 # Arguments : |
93 # object : object to be deleted |
94 # |
95 def self.delete_object(object) |
96 object = nil |
97 end |
98 |
99 # Description : extracts the features module and submodule names from a given file path |
100 # Author : Chandra sekaran |
101 # Arguments : |
102 # str_file_path : absolute path of the feature file |
103 # Return value : |
104 # str_feature_dir: string value of module/submodule name |
105 # |
106 def get_feature_module_name(str_file_path) |
107 str_file_path = format_file_path(str_file_path) |
108 str_file_path = str_file_path.split("features/").last # extracts all strings after 'features\' |
109 #puts str_file_path |
110 arr_file_dirs = str_file_path.split("/") # split the strings into array based on '\' |
111 str_feature_dir = "" |
112 |
113 arr_file_dirs.pop # removes the last element i.e., feature file |
114 # arr_file_dirs.pop # removes the second last element i.e., test case folder |
115 (0..arr_file_dirs.size - 1).each do |num_counter| |
116 str_feature_dir << "_" if num_counter > 0 |
117 str_feature_dir << "#{arr_file_dirs[num_counter]}" # form a new string from array values |
118 end |
119 str_feature_dir |
120 rescue Exception => ex |
121 $log.error("Error in extracting features directory : #{ex}") |
122 exit |
123 end |
124 |
125 # Description : Renames the created html/json report file and moves it to the current log directory |
126 # Author : Chandra sekaran |
127 # |
128 def create_html_report |
129 str_timestamp = $log_env.get_formatted_datetime($end_time) |
130 # rename and move html report file into current test report directory |
131 File.rename("#{$REPORT_FILE_NAME}.html", "report_#{str_timestamp}.html") |
132 FileUtils.mv("report_#{str_timestamp}.html", $current_log_dir) |
133 # rename and move json report file into current test report directory |
134 File.rename("#{$REPORT_FILE_NAME}.json", "report_#{str_timestamp}.json") |
135 FileUtils.mv("report_#{str_timestamp}.json", $current_log_dir) |
136 $log.info("Html/JSON report files created and saved in '#{$current_log_dir}'") |
137 rescue Exception => ex |
138 $log.error("Error in creating html report : #{ex}") |
139 exit |
140 end |
141 |
142 # Description : renames all files extension under given file path |
143 # Author : Chandra sekaran |
144 # Arguments : |
145 # str_file_path : absolute path of file |
146 # str_file_type : new file extension to be created |
147 # |
148 def rename_file_type(str_file_path, str_file_type) |
149 Dir.glob(str_file_path).each do |file| |
150 FileUtils.mv file, "#{File.dirname(file)}/#{File.basename(file,'.*')}.#{str_file_type}" |
151 end |
152 $log.info("File type(s) under '#{str_file_path}' renamed successfully to '#{str_file_type}'") |
153 rescue Exception => ex |
154 $log.error("Error in renaming '#{str_file_path}' to type '#{str_file_type}': #{ex}") |
155 exit |
156 end |
157 |
158 # Description : get files which are all created under given file path with extension and after given timestamp |
159 # Author : Gomathi |
160 # Arguments : |
161 # str_file_path : absolute path of file |
162 # str_file_type : file extension |
163 # obj_time_stamp : execution start time as time object |
164 # Return argument : |
165 # arr_files : array of file path(s) |
166 # |
167 def get_files_absolute_path(str_file_path, str_file_type, obj_time_stamp) |
168 arr_abs_path = Dir["#{str_file_path}/*/*.#{str_file_type}"] |
169 arr_files = [] |
170 arr_abs_path.each do |file_path| |
171 arr_files << file_path if File.ctime(file_path) > obj_time_stamp |
172 end |
173 arr_files |
174 rescue Exception => ex |
175 $log.error("Error in getting '#{str_file_type}' file(s) under '#{str_file_path}' for '#{obj_time_stamp}' : #{ex}") |
176 exit |
177 end |
178 |
179 # Description : formats the file path by replacing "\" with "/" |
180 # Author : Chandra sekaran |
181 # Arguments : |
182 # str_fileabs_path : absolute path of file |
183 # Return value : |
184 # str_file_path : formatted absolute path of file |
185 # |
186 def format_file_path(str_fileabs_path) |
187 str_file_path = str_fileabs_path |
188 str_file_path.each_char do |letter| # replace all the escape sequences |
189 case letter |
190 when /[\a]/ |
191 str_file_path[letter] = "/a" |
192 when /[\e]/ |
193 str_file_path[letter] = "/e" |
194 when /[\b]/ |
195 str_file_path[letter] = "/b" |
196 when /[\cx]/ |
197 str_file_path[letter] = "/cx" |
198 when /[\f]/ |
199 str_file_path[letter] = "/f" |
200 when /[\n]/ |
201 str_file_path[letter] = "/n" |
202 when /[\nnn]/ |
203 #str_file_path[letter] = "/nnn" # not required as \n is given |
204 when /[\r]/ |
205 str_file_path[letter] = "/r" |
206 when /[\s]/ |
207 str_file_path[letter] = "/t" # it is taking "\t" as "\s" |
208 when /[\t]/ |
209 str_file_path[letter] = "/t" |
210 when "\\" |
211 str_file_path[letter] = "/" |
212 #when /[\v]/ # not required due to expression error |
213 #str_file_path[letter] = "/v" # not required due to expression error |
214 #when /[\x]/ # not required due to expression error |
215 #str_file_path[letter] = "/x" # not required due to expression error |
216 #when /[\xnn]/ # not required due to expression error |
217 #str_file_path[letter] = "/xnn" # not required due to expression error |
218 end |
219 end |
220 return str_file_path |
221 rescue Exception => ex |
222 $log.error("Error in formatting file path (#{str_file_path}) : #{ex}") |
223 exit |
224 end |
225 |
226 # Description : extracts local file name and sets the directory path for DataMagic to load the data file for the current scenario/step |
227 # Author : Chandra sekaran |
228 # Arguments : |
229 # str_datafile_name : name of data file |
230 # Return value : |
231 # : hash of the loaded yml file |
232 # |
233 def set_scenario_based_datafile(str_global_file_name) |
234 str_local_file_name = str_global_file_name |
235 $scenario_tags.each do |tag| |
236 if tag.include? "tc" |
237 tag_name = tag.gsub("@", "_") |
238 tmp = str_global_file_name.gsub(".", "#{tag_name}.") |
239 str_local_file_name = tmp |
240 break |
241 end |
242 end |
243 str_file_name = set_datafile_path(str_local_file_name, str_global_file_name) |
244 DataMagic.load(str_file_name) # returns yml content as a hash |
245 rescue Exception => ex |
246 $log.error("Error in setting scenario based data file for (#{str_global_file_name}) : #{ex}") |
247 exit |
248 end |
249 |
250 # Description : sets the directory path for DataMagic to load the data file for the current scenario/step |
251 # Author : Chandra sekaran |
252 # Arguments : |
253 # str_local_file_name : name of local data file |
254 # str_global_file_name: name of global data file |
255 # Return value : |
256 # actual_file : name of the required file |
257 # |
258 def set_datafile_path(str_local_file_name, str_global_file_name) |
259 str_feature_file_path = format_file_path($str_feature_file_path) |
260 arr_temp = str_feature_file_path.split("/") |
261 arr_temp.pop |
262 arr_temp.push(str_local_file_name) |
263 str_datafile_dir = arr_temp * "/" |
264 actual_file = "" |
265 if File.exists?(str_datafile_dir) |
266 arr_temp_dir = str_datafile_dir.split('/') |
267 arr_temp_dir.pop |
268 @str_temp_dir = arr_temp_dir * "/" |
269 DataMagic.yml_directory = @str_temp_dir |
270 actual_file = str_local_file_name |
271 else |
272 arr_temp_dir = str_datafile_dir.split('/') |
273 arr_temp_dir.pop |
274 arr_temp_dir.pop |
275 arr_temp_dir.push("test_data") |
276 @str_temp_dir = arr_temp_dir * "/" |
277 DataMagic.yml_directory = @str_temp_dir |
278 actual_file = str_global_file_name |
279 end |
280 puts "Test data file successfully set to #{@str_temp_dir}" |
281 return actual_file |
282 rescue Exception => ex |
283 $log.error("Error in setting data file path (#{str_local_file_name}/#{str_global_file_name}) : #{ex}") |
284 exit |
285 end |
286 |
287 # Description : execute the kernel command |
288 # Author : Chandra sekaran |
289 # Arguments : |
290 # str_command : command string |
291 # Return value : a boolean value |
292 # |
293 def execute_command(str_command) |
294 str_stdout, str_stderr = '', '' |
295 Open3.popen3(str_command) do |i,o,e| |
296 i.close |
297 while((line = o.gets)) |
298 str_stdout << line |
299 end |
300 while((line = e.gets)) |
301 str_stderr << line |
302 end |
303 end |
304 $log.success("STDOUT : #{str_stdout.strip}") if !(str_stdout.nil? || str_stdout.empty?) |
305 raise str_stderr.strip if !(str_stderr.nil? || str_stderr.empty?) |
306 return true |
307 rescue Exception => ex |
308 $log.error("Error while executing the command (#{str_command}) : #{ex}") |
309 exit |
310 end |
311 |
312 end |
313 end |
Generated on 2015-05-08 10:40:30 +0530 with SimpleCov-RCov 0.2.3