| Name | Total Lines | Lines of Code | Total Coverage | Code Coverage |
|---|---|---|---|---|
| app/helpers/ensure_config_helper.rb | 143 | 97 | 44.06%
|
25.77%
|
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.
1 require 'open3' # required for popen3 |
2 |
3 module EnsureConfigHelper |
4 |
5 =begin |
6 - repository directory exists |
7 - is writable/readable |
8 - VALIDATE_FILE |
9 - is executable |
10 - logging |
11 - MARKUS_LOGGING_LOGFILE's parent dir is writable |
12 - MARKUS_LOGGING_ERRORLOGFILE's parent dir is writable |
13 =end |
14 # check directory instead of file because, the logger has not created |
15 # the file yet |
16 def self.check_config() |
17 check_in_readable_dir(MarkusConfigurator.markus_config_logging_logfile, "MARKUS_LOGGING_LOGFILE") |
18 check_in_writable_dir(MarkusConfigurator.markus_config_logging_logfile, "MARKUS_LOGGING_LOGFILE") |
19 check_in_executable_dir(MarkusConfigurator.markus_config_logging_logfile, "MARKUS_LOGGING_LOGFILE") |
20 check_in_readable_dir(MarkusConfigurator.markus_config_logging_errorlogfile, "MARKUS_LOGGING_ERRORLOGFILE") |
21 check_in_writable_dir(MarkusConfigurator.markus_config_logging_errorlogfile, "MARKUS_LOGGING_ERRORLOGFILE") |
22 check_in_executable_dir(MarkusConfigurator.markus_config_logging_errorlogfile, "MARKUS_LOGGING_ERRORLOGFILE") |
23 check_writable(MarkusConfigurator.markus_config_repository_storage, "REPOSITORY_STORAGE") |
24 check_readable(MarkusConfigurator.markus_config_repository_storage, "REPOSITORY_STORAGE") |
25 check_executable(MarkusConfigurator.markus_config_repository_storage, "REPOSITORY_STORAGE") |
26 check_writable(MarkusConfigurator.markus_config_pdf_storage, "PDF_STORAGE") |
27 check_readable(MarkusConfigurator.markus_config_pdf_storage, "PDF_STORAGE") |
28 check_in_writable_dir(MarkusConfigurator.markus_config_automated_tests_repository, "automated_tests_REPOSITORY") |
29 check_configured_default_language(MarkusConfigurator.markus_config_default_language) |
30 ensure_logout_redirect_link_valid |
31 if ! ( RUBY_PLATFORM =~ /(:?mswin|mingw)/ ) # should match for Windows only |
32 check_if_executes( MarkusConfigurator.markus_config_validate_file, "VALIDATE_FILE") |
33 end |
34 end |
35 |
36 # Checks if language file for configured default |
37 # language is present. |
38 def self.check_configured_default_language( lang ) |
39 available_languages = Dir.glob(File.join( ::Rails.root.to_s, "config", "locales", "*.yml" )) |
40 available_languages = available_languages.collect{ |file| File.basename(file).chomp(".yml") } |
41 if !available_languages.include?( lang ) |
42 raise ( "Language file #{lang}.yml does not exist in config/locales. Please " + |
43 "make sure that MARKUS_DEFAULT_LANGUAGE is configured correctly in " + |
44 "config/environments/#{Rails.env}.rb and language file is present." ) |
45 end |
46 end |
47 |
48 # checks if the given file's directory is writable |
49 # and raises an exception if it is not |
50 def self.check_in_writable_dir( filename, constant_name ) |
51 dir = Pathname.new( filename ).dirname |
52 if ! File.writable?(dir) |
53 raise ( "The setting #{constant_name} with path #{dir} is not writable. Please double" + |
54 " check the setting in config/environments/#{Rails.env}.rb" ) |
55 end |
56 end |
57 |
58 # checks if the given file's directory is readable |
59 # and raises an exception if it is not |
60 def self.check_in_readable_dir( filename, constant_name ) |
61 dir = Pathname.new( filename ).dirname |
62 if ! File.readable?( dir ) |
63 raise ( "The setting #{constant_name} with path #{dir} is not readable. Please double" + |
64 " check the setting in config/environments/#{Rails.env}.rb" ) |
65 end |
66 end |
67 |
68 # checks if the given file's directory is executable |
69 # and raises an exception if it is not |
70 def self.check_in_executable_dir( filename, constant_name ) |
71 dir = Pathname.new( filename ).dirname |
72 if ! File.executable?( dir ) |
73 raise ( "The setting #{constant_name} with path #{dir} is not executable. Please double " + |
74 "check the setting in config/environments/#{Rails.env}.rb" ) |
75 end |
76 end |
77 |
78 # checks if the given file is writable and raises |
79 # an exception if it is not |
80 def self.check_writable( filename, constant_name ) |
81 if ! File.writable?(filename) |
82 raise ( "The setting #{constant_name} with path #{filename} is not writable. Please double" + |
83 " check the setting in config/environments/#{Rails.env}.rb" ) |
84 end |
85 end |
86 |
87 # checks if the given file is readable and raises |
88 # an exception if it is not |
89 def self.check_readable( filename, constant_name ) |
90 if ! File.readable?(filename) |
91 raise ( "The setting #{constant_name} with path #{filename} is not readable. Please double" + |
92 " check the setting in config/environments/#{Rails.env}.rb" ) |
93 end |
94 end |
95 |
96 # checks if the given file is executable and raises |
97 # an exception if it is not. |
98 def self.check_executable( filename, constant_name ) |
99 if ! File.executable?(filename) |
100 raise ( "The setting #{constant_name} with path #{filename} is not executable. Please double " + |
101 "check the setting in config/environments/#{Rails.env}.rb" ) |
102 end |
103 end |
104 |
105 # checks if the given file executes succesfully |
106 def self.check_if_executes( filename, constant_name ) |
107 p_stdin, p_stdout, p_stderr = Open3.popen3( filename ) |
108 p_stdin.puts("test\ntest") # write to stdin of markus_config_validate |
109 p_stdin.close |
110 # HACKALARM: |
111 # Disconnect from DB before reading from stderr. PostgreSQL gets confused |
112 # if we don't do this. Since these checks run on server startup/shutdown |
113 # only, this should be OK. |
114 con_identifier = ActiveRecord::Base.remove_connection |
115 error = p_stderr.read |
116 # Get DB connection back |
117 ActiveRecord::Base.establish_connection(con_identifier) |
118 if error.length != 0 |
119 if error =~ /(Errno::ENOENT)|(Permission denied)/ |
120 raise ( "The setting #{constant_name} with path #{filename} is not executable. Please double " + |
121 "check the setting in config/environments/#{Rails.env}.rb" ) |
122 else |
123 # This may not indicate an error (maybe just authentication failed and something |
124 # was printed to stderr). Log this, but do no more. |
125 $stderr.puts "Error writing to pipe. #{filename}, #{error}. Please double check the" + |
126 " setting in config/environments/#{Rails.env}.rb" |
127 end |
128 end |
129 end |
130 |
131 def self.ensure_logout_redirect_link_valid |
132 logout_redirect = MarkusConfigurator.markus_config_logout_redirect |
133 if ["DEFAULT", "NONE"].include?(logout_redirect) |
134 return |
135 #We got a URI, ensure its of proper format <> |
136 elsif logout_redirect.match('^http://|^https://').nil? |
137 raise ( "LOGOUT_REDIRECT value #{logout_redirect} is invalid. Only 'DEFAULT', " + |
138 "'NONE' or addresses beginning with http:// or https:// are valid values. " + |
139 "Please double check configuration in config/environments/#{Rails.env}.rb" ) |
140 end |
141 end |
142 |
143 end |
Generated on Tue Feb 07 00:07:35 -0500 2012 with rcov 0.9.10