diff --git a/ext/fiparse/test/test_fachinfo_hpricot.rb b/ext/fiparse/test/test_fachinfo_hpricot.rb index 634edee98..b6183e893 100755 --- a/ext/fiparse/test/test_fachinfo_hpricot.rb +++ b/ext/fiparse/test/test_fachinfo_hpricot.rb @@ -19,6 +19,7 @@ require 'plugin/text_info' require 'stub/cgi' require 'flexmock/minitest' +require 'util/workdir' module ODDB class FachinfoDocument @@ -27,6 +28,8 @@ def odba_id end end module FiParse + HTML_DIR = File.join(ODDB::PROJECT_ROOT, 'ext/fiparse/test/data/html') + HTML_PREFIX = '' HTML_POSTFIX = '' if true @@ -361,7 +364,7 @@ class TestFachinfoHpricotAlcaCDe default_config_files, - 'data_dir' => File.expand_path('../data', File.dirname(__FILE__)), - 'log_dir' => File.expand_path('../log', File.dirname(__FILE__)), - 'log_pattern' => File.join(Dir.pwd, defined?(MiniTest) ? 'test/log' : 'log','/%Y/%m/%d/app_log'), + 'data_dir' => ODDB::WORK_DIR, + 'log_dir' => ODDB::LOG_DIR, + 'log_pattern' => File.join(ODDB::LOG_DIR, '/%Y/%m/%d/app_log'), 'url_bag_sl_zip' => 'http://www.xn--spezialittenliste-yqb.ch/File.axd?file=XMLPublications.zip', 'bsv_archives' => '(?:PR|BSV_per_20)(0[3-8])[\d.]+(?:txt|xls)', 'server_url' => SERVER_URI, diff --git a/src/plugin/csv_export.rb b/src/plugin/csv_export.rb index 3fd575b87..49d813560 100644 --- a/src/plugin/csv_export.rb +++ b/src/plugin/csv_export.rb @@ -8,12 +8,12 @@ require 'oddb2tdat' require 'model/package' require 'model/atcclass' +require 'util/workdir' module ODDB class CsvExportPlugin < Plugin EXPORT_SERVER = DRbObject.new(nil, EXPORT_URI) - EXPORT_DIR = File.join(ARCHIVE_PATH, 'downloads') - MIGEL_EXPORT_DIR = File.expand_path('../../../migel/data/csv', File.dirname(__FILE__)) + MIGEL_EXPORT_DIR = File.join(ODDB::PROJECT_ROOT, 'migel/data/csv') ODDB_RECIPIENTS = ['oddb'] ODDB_RECIPIENTS_DAT = ['oddb_dat'] ODDB_RECIPIENTS_EXTENDED = ['oddb_dat_extended'] @@ -58,7 +58,7 @@ def _export_drugs(export_name, keys) @counts = exporter.counts EXPORT_SERVER.compress(EXPORT_DIR, name) backup = @app.log_group(:bsv_sl).newest_date.strftime("#{export_name}.%Y-%m-%d.csv") - backup_dir = File.expand_path('../../data/csv', File.dirname(__FILE__)) + backup_dir = File.join(ODDB::WORK_DIR, 'csv') backup_path = File.join(backup_dir, backup) unless(File.exist? backup_path) FileUtils.mkdir_p(backup_dir) @@ -131,7 +131,7 @@ def export_fachinfo_chapter(term, chapters, lang, file) 'Chapters' => chapters.join(','), } EXPORT_SERVER.compress(EXPORT_DIR, file) - backup_dir = File.expand_path('../../data/csv', File.dirname(__FILE__)) + backup_dir = File.join(ODDB::WORK_DIR, 'csv') backup_path = File.join(backup_dir, file) unless(File.exist? backup_path) FileUtils.mkdir_p(backup_dir) @@ -265,7 +265,7 @@ def export_teilbarkeit @counts = exporter.divisions EXPORT_SERVER.compress(EXPORT_DIR, name) backup = Date.today.strftime("#{export_name}.%Y-%m-%d.csv") - backup_dir = File.expand_path('../../data/csv', File.dirname(__FILE__)) + backup_dir = File.join(ODDB::WORK_DIR, 'csv') backup_path = File.join(backup_dir, backup) unless(File.exist? backup_path) FileUtils.mkdir_p(backup_dir) @@ -295,7 +295,7 @@ def export_flickr_photo @counts = exporter.flickr_photos EXPORT_SERVER.compress(EXPORT_DIR, name) backup = Date.today.strftime("#{export_name}.%Y-%m-%d.csv") - backup_dir = File.expand_path('../../data/csv', File.dirname(__FILE__)) + backup_dir = File.join(ODDB::WORK_DIR, 'csv') backup_path = File.join(backup_dir, backup) unless(File.exist? backup_path) FileUtils.mkdir_p(backup_dir) diff --git a/src/plugin/mail_order_price.rb b/src/plugin/mail_order_price.rb index cfba72116..03fc70be5 100644 --- a/src/plugin/mail_order_price.rb +++ b/src/plugin/mail_order_price.rb @@ -10,7 +10,7 @@ module ODDB class MailOrderPricePlugin < Plugin - CSV_DIR = File.expand_path('../../data/csv', File.dirname(__FILE__)) + CSV_DIR = File.join(WORK_DIR, 'csv') def report lines = [ "Updated Packages: #{@updated_packages.length}", diff --git a/src/plugin/plugin.rb b/src/plugin/plugin.rb index 18a81af1a..82354106c 100644 --- a/src/plugin/plugin.rb +++ b/src/plugin/plugin.rb @@ -34,7 +34,7 @@ def method_missing(*args) SWISSMEDIC_HUMAN_URL_FR = 'https://www.swissmedic.ch//swissmedic/fr/medicaments-a-usage-humain' SWISSMEDIC_HUMAN_URL_IT = 'https://www.swissmedic.ch//swissmedic/it/home/medicamenti-per-uso-umano' include HttpFile - ARCHIVE_PATH = File.expand_path('../../data', File.dirname(__FILE__)) + ARCHIVE_PATH = ODDB::WORK_DIR # Recipients for Plugin-Specific Update-Logs can be added in # 'Plugin's subclasses RECIPIENTS = [] diff --git a/src/plugin/refdata_jur.rb b/src/plugin/refdata_jur.rb index e958abcd1..f5de50c40 100644 --- a/src/plugin/refdata_jur.rb +++ b/src/plugin/refdata_jur.rb @@ -37,7 +37,7 @@ def self.company_address_matches(new_addr, old_addr) def self.download_partners_xml xml = nil begin - file2save = File.join(ODDB.config.data_dir, 'xml', 'refdata_jur.xml') + file2save = File.join(ODDB::WORK_DIR, 'xml', 'refdata_jur.xml') FileUtils.rm_f(file2save, verbose: false) @client = Savon.client(wsdl: "https://refdatabase.refdata.ch/Service/Partner.asmx?WSDL") # TYPE Search Type @@ -78,8 +78,8 @@ def self.setup_default_agent agent end DebugImport = defined?(MiniTest) - Companies_XML = File.expand_path(File.join(__FILE__, '../../../data/xml/refdata_jur_latest.xml')) - Companies_curr = File.expand_path(File.join(__FILE__, "../../../data/xml/refdata_jur_#{Time.now.strftime('%Y.%m.%d')}.xml")) + Companies_XML = File.join(ODDB::WORK_DIR, 'xml/refdata_jur_latest.xml') + Companies_curr = File.join(ODDB::WORK_DIR, "xml/refdata_jur_#{Time.now.strftime('%Y.%m.%d')}.xml") # MedRegURL = 'http://www.medregom.admin.ch/' # role_types are => ["Pharm", "Indus", "Hosp", "DruSto", "SerFirm", "DoctMed", "PubHea", "Whole", "Pharmst", "Inst", "HeaIns", "IntOrg", "HeaEmpl", "NursHom", "ONursOrg", "SWFirm", "EmergServ", "Assoc", "NonHealthCare", "HeaTec", "AccIns", "HeaProd", "SpecPra", "Drugg", "GrpPra", "Dent", "Veter", "Nurse", "Lab", "Chiro", "HeaProv", "Physio", "LabLeader", "Midw", "Psycho", "Naturopath", "NutrAdv", "SocSec", "Spitex", "DentGrpPra", "CompTherapist", "VetGrpPra", "PrivPra", "Ergo", "MedPracAss", "DiabAdv", "SpeeTher", "PharmAss", "MedSecr", "EmergCent"] diff --git a/src/plugin/refdata_nat.rb b/src/plugin/refdata_nat.rb index 468168939..a18967187 100644 --- a/src/plugin/refdata_nat.rb +++ b/src/plugin/refdata_nat.rb @@ -29,7 +29,7 @@ module Doctors def self.download_doctors_xml xml = nil begin - file2save = File.join(ODDB.config.data_dir, 'xml', 'refdata_nat.xml') + file2save = File.join(ODDB::WORK_DIR, 'xml', 'refdata_nat.xml') FileUtils.rm_f(file2save, verbose: false) @client = Savon.client(wsdl: "https://refdatabase.refdata.ch/Service/Partner.asmx?WSDL") # TYPE Search Type @@ -71,8 +71,8 @@ def self.setup_default_agent agent end DebugImport = defined?(MiniTest) - Doctors_XML = File.expand_path(File.join(__FILE__, '../../../data/xml/refdata_nat_latest.xml')) - Doctors_curr = File.expand_path(File.join(__FILE__, "../../../data/xml/refdata_nat_#{Time.now.strftime('%Y.%m.%d')}.xml")) + Doctors_XML = File.join(ODDB::WORK_DIR, 'xml/refdata_nat_latest.xml') + Doctors_curr = File.join(ODDB::WORK_DIR, "xml/refdata_nat_#{Time.now.strftime('%Y.%m.%d')}.xml") # MedRegURL = 'http://www.medregom.admin.ch/' # role_types are => ["Pharm", "Indus", "Hosp", "DruSto", "SerFirm", "DoctMed", "PubHea", "Whole", "Pharmst", "Inst", "HeaIns", "IntOrg", "HeaEmpl", "NursHom", "ONursOrg", "SWFirm", "EmergServ", "Assoc", "NonHealthCare", "HeaTec", "AccIns", "HeaProd", "SpecPra", "Drugg", "GrpPra", "Dent", "Veter", "Nurse", "Lab", "Chiro", "HeaProv", "Physio", "LabLeader", "Midw", "Psycho", "Naturopath", "NutrAdv", "SocSec", "Spitex", "DentGrpPra", "CompTherapist", "VetGrpPra", "PrivPra", "Ergo", "MedPracAss", "DiabAdv", "SpeeTher", "PharmAss", "MedSecr", "EmergCent"] diff --git a/src/plugin/shortage.rb b/src/plugin/shortage.rb index 3b90ac75d..207ee5993 100644 --- a/src/plugin/shortage.rb +++ b/src/plugin/shortage.rb @@ -15,7 +15,6 @@ module ODDB class ShortagePlugin < Plugin - EXPORT_DIR = File.join(ARCHIVE_PATH, 'downloads') BASE_URI = 'https://www.drugshortage.ch' SOURCE_URI = BASE_URI + '/UebersichtaktuelleLieferengpaesse2.aspx' NoMarketingSource = 'https://www.swissmedic.ch/dam/swissmedic/de/dokumente/internetlisten/meldungen_art11_ham.xlsx.download.xlsx/Liste%20Meldungen%2011%20VAM.xlsx' @@ -25,15 +24,16 @@ def initialize app, opts={:reparse => false} @@logInfo = [] @options = opts @options ||= {} - @latest_shortage = File.expand_path('../../data/html/drugshortage-latest.html', File.dirname(__FILE__)) - @latest_nomarketing = File.expand_path('../../data/xlsx/nomarketing-latest.xlsx', File.dirname(__FILE__)) - @csv_file_path = File.join EXPORT_DIR, 'drugshortage.csv' - @yesterday_csv_file_path = File.join EXPORT_DIR, "drugshortage-#{(@@today-1).strftime("%Y.%m.%d")}.csv" - @dated_csv_file_path = File.join EXPORT_DIR, "drugshortage-#{@@today.strftime("%Y.%m.%d")}.csv" + @latest_shortage = File.join(ODDB::WORK_DIR, 'html/drugshortage-latest.html') + @latest_nomarketing = File.join(ODDB::WORK_DIR, 'data/xlsx/nomarketing-latest.xlsx') + @csv_file_path = File.join(ODDB::EXPORT_DIR, 'drugshortage.csv') + @yesterday_csv_file_path = File.join(ODDB::EXPORT_DIR, "drugshortage-#{(@@today-1).strftime("%Y.%m.%d")}.csv") + @dated_csv_file_path = File.join(ODDB::EXPORT_DIR, "drugshortage-#{@@today.strftime("%Y.%m.%d")}.csv") @report_shortage = [] @report_nomarketing = [] - FileUtils.rm_f(@latest_shortage, verbose: true) if @options[:reparse] && File.exist?(@latest_shortage) - FileUtils.rm_f(@latest_nomarketing, verbose: true) if @options[:reparse] && File.exist?(@latest_nomarketing) + LogFile.debug "#{Latest.get_daily_name(@latest_shortage)}" + FileUtils.rm_f(@latest_shortage) if @options[:reparse] && File.exist?(@latest_shortage) + FileUtils.rm_f(@latest_nomarketing) if @options[:reparse] && File.exist?(@latest_nomarketing) end def date @@today @@ -81,7 +81,7 @@ def export_drugshortage_csv values = [] values << info.gtin unless @app.package_by_ean13(info.gtin) - puts "Skipping non existing package #{info.gtin}" + LogFile.debug "Skipping non existing package #{info.gtin}" next end atc_class = @app.package_by_ean13(info.gtin).atc_class @@ -100,6 +100,7 @@ def export_drugshortage_csv csv << values end end + LogFile.debug "rm #{@yesterday_csv_file_path}" FileUtils.rm_f(@yesterday_csv_file_path) if File.exist?(@yesterday_csv_file_path) && IO.read(@yesterday_csv_file_path).eql?(IO.read(@csv_file_path)) @csv_file_path end @@ -118,6 +119,7 @@ def log_info private def report_shortage unless @shortages && @shortages.size > 0 + LogFile.debug "rm #{Latest.get_daily_name(@latest_shortage)}" FileUtils.rm_f(Latest.get_daily_name(@latest_shortage)) @report_summary << "Nothing changed in #{SOURCE_URI}" return @@ -133,12 +135,14 @@ def report_shortage if @deleted_shortages.size > 0 || @changes_shortages.size > 0 @has_relevant_changes = true else - FileUtils.rm_f(Latest.get_daily_name(@latest_shortage), verbose: true) + LogFile.debug "#{Latest.get_daily_name(@latest_shortage)}" + FileUtils.rm_f(Latest.get_daily_name(@latest_shortage)) end end def report_nomarketing unless @found_nomarketings && @found_nomarketings.size > 0 - FileUtils.rm_f(Latest.get_daily_name(@latest_nomarketing), verbose: true) + LogFile.debug "#{Latest.get_daily_name(@latest_nomarketing)}" + FileUtils.rm_f(Latest.get_daily_name(@latest_nomarketing)) @report_summary << "Nothing changed in #{@nomarketing_href}" return end diff --git a/src/plugin/text_info.rb b/src/plugin/text_info.rb index 6aba2a1e3..bb489a6f4 100644 --- a/src/plugin/text_info.rb +++ b/src/plugin/text_info.rb @@ -824,7 +824,11 @@ def TextInfoPlugin.find_iksnr_in_string(string, iksnr) end def download_swissmedicinfo_xml(file = nil) - return IO.read(file) if file + if file + content = IO.read(file) + LogFile.debug("Read #{content.length} bytes from #{file}") + return content + end setup_default_agent url = "http://download.swissmedicinfo.ch/Accept.aspx?ReturnUrl=%2f" dir = File.join(ODDB.config.data_dir, 'xml') @@ -1391,7 +1395,10 @@ def parse_aips_download @aips_xml = @options[:xml_file] if @options[:xml_file] # FileUtils.rm_rf(@details_dir, verbose: true) # spart etwas Zeit und lässt alte Dokus zu FileUtils.makedirs(@details_dir, verbose: true) - return unless File.exist?(@aips_xml) + unless File.exist?(@aips_xml) + LogFile.debug("Did not find #{@aips_xml}") + return + end content = IO.read(@aips_xml, :encoding => 'UTF-8') LogFile.debug "read #{@aips_xml} #{content.size} bytes" content.split('').each do |chunk| diff --git a/src/state/global.rb b/src/state/global.rb index e98ef92eb..aa15a69e2 100644 --- a/src/state/global.rb +++ b/src/state/global.rb @@ -83,6 +83,7 @@ require 'util/umlautsort' require 'plugin/rss' require 'sbsm/state' +require 'util/workdir' module ODDB @@ -535,8 +536,7 @@ def proceed_download keys = [:download, :months, :compression] input = user_input(keys, keys) items = [] - dir = File.expand_path('../../data/downloads', - File.dirname(__FILE__)) + dir = ODDB::EXPORT_DIR compression = input[:compression] if(files = input[:download]) files.each { |filename, val| diff --git a/src/state/user/download_item.rb b/src/state/user/download_item.rb index 90301e41c..a2670e8c1 100644 --- a/src/state/user/download_item.rb +++ b/src/state/user/download_item.rb @@ -9,6 +9,7 @@ require 'state/user/download_export' require 'view/user/export' require 'view/user/download_item' +require 'util/workdir' module ODDB module State @@ -21,7 +22,7 @@ def init keys = [:buy, :month, :compression] # month and compression are optional input = user_input(keys, [:buy]) items = [] # as container (one item) - dir = File.expand_path('../../../data/downloads', File.dirname(__FILE__)) + dir = ODDB::TEST_DATA_DIR filename = nil # All files have .zip compressed version without epub and prc. # Some items have only .zip and .tar.gz only. diff --git a/src/util/csstemplate.rb b/src/util/csstemplate.rb index ff1ef7471..7542a3a1e 100644 --- a/src/util/csstemplate.rb +++ b/src/util/csstemplate.rb @@ -3,11 +3,12 @@ # ODDB::CssTemplate -- oddb.org -- 04.10.2012 -- yasaka@ywesee.com require 'fileutils' +require 'util/workdir' module ODDB class CssTemplate - RESOURCE_PATH = "../../doc/resources/" - TEMPLATE = File.expand_path('../../data/css/template.css', File.dirname(__FILE__)) + RESOURCE_PATH = File.join(ODDB::PROJECT_ROOT, 'doc/resources') + TEMPLATE = File.join(ODDB::PROJECT_ROOT, 'data/css/template.css') FLAVORS = { :desitin => { :bg_dark => '#1b49a2', @@ -316,7 +317,6 @@ def write_css() fh << substitute(src, updates) } File.chmod(0664, path) - puts path end } end diff --git a/src/util/dir.rb b/src/util/dir.rb deleted file mode 100644 index 177ad9d97..000000000 --- a/src/util/dir.rb +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env ruby -# encoding: utf-8 -# Dir -- ODDB -- 21.10.2003 -- hwyss@ywesee.com - -class Dir - def Dir.mkdir_r(name) - parent = File.dirname(name) - unless(File.exist?(parent)) - self.mkdir_r(parent) - end - mkdir(name) - end -end diff --git a/src/util/exporter.rb b/src/util/exporter.rb index fa57fa84d..fa4081987 100644 --- a/src/util/exporter.rb +++ b/src/util/exporter.rb @@ -11,13 +11,12 @@ require 'util/logfile' require 'util/schedule' require 'util/mail' +require 'util/workdir' module ODDB class Exporter include Util::Schedule EXPORT_SERVER = DRbObject.new(nil, EXPORT_URI) - EXPORT_DIR = File.expand_path('../../data/downloads', - File.dirname(__FILE__)) FileUtils.mkdir_p(EXPORT_DIR) class SessionStub attr_accessor :language, :flavor, :lookandfeel @@ -183,8 +182,7 @@ def export_pdf end def export_sl_pcodes safe_export 'sl_pcodes.txt' do - path = File.expand_path('../../data/txt/sl_pcodes.txt', - File.dirname(__FILE__)) + path = File.join(WORK_DIR, 'txt/sl_pcodes.txt') File.open(path, 'w') { |fh| @app.each_package { |pac| if(pac.sl_entry && pac.pharmacode) @@ -273,7 +271,7 @@ def export_teilbarkeit_csv log.date_str = today.strftime("%d.%m.%Y") log.report = report file = today.strftime("teilbarkeit.%Y-%m-%d.csv") - dir = File.expand_path('../../data/csv', File.dirname(__FILE__)) + dir = File.join(WORK_DIR, 'csv') path = File.join(dir, file) log.files = { path => ['text/csv'] } log.notify('Teilbarkeit Export') @@ -290,7 +288,7 @@ def export_flickr_photo_csv log.date_str = today.strftime("%d.%m.%Y") log.report = report file = today.strftime("flickr_ean_export.%Y-%m-%d.csv") - dir = File.expand_path('../../data/csv', File.dirname(__FILE__)) + dir = File.join(WORK_DIR, 'csv') path = File.join(dir, file) log.files = { path => ['text/csv'] } log.notify('Flickr Ean Export') diff --git a/src/util/logfile.rb b/src/util/logfile.rb index d49c76d97..08c97a103 100644 --- a/src/util/logfile.rb +++ b/src/util/logfile.rb @@ -8,11 +8,7 @@ module ODDB module LogFile - if defined?(MiniTest) - LOG_ROOT = ODDB.config.log_dir = File.join(PROJECT_ROOT, 'test', 'log') - else - LOG_ROOT = File.join(PROJECT_ROOT, 'log') - end + LOG_ROOT = File.join(WORK_DIR, 'log') def append(key, line, time=Time.now.utc) file = filename(key, time) dir = File.dirname(file) diff --git a/src/util/mail.rb b/src/util/mail.rb index 05fe7b56d..1ff15d79c 100644 --- a/src/util/mail.rb +++ b/src/util/mail.rb @@ -5,12 +5,15 @@ require 'config' require 'util/logfile' require 'yaml' +$: << File.expand_path("../../src", File.dirname(__FILE__)) +require 'util/workdir' + module ODDB module Util # see also the file test/data/oddb_mailing_test.yml - MailingTestConfiguration = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'test', 'data', 'oddb_mailing_test.yml')) - MailingDefaultConfiguration = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'etc', 'oddb.yml')) + MailingTestConfiguration = File.join(PROJECT_ROOT, 'test', 'data', 'oddb_mailing_test.yml') + MailingDefaultConfiguration = File.join(PROJECT_ROOT, 'etc', 'oddb.yml') MailingListIds = 'mailing_list_ids' MailingRecipients = 'mail_recipients' @mail_configured = false diff --git a/src/util/oddbconfig.rb b/src/util/oddbconfig.rb index 2ef2feaca..eba78c53b 100644 --- a/src/util/oddbconfig.rb +++ b/src/util/oddbconfig.rb @@ -5,14 +5,15 @@ # OddbConfig -- oddb.org -- 09.04.2003 -- hwyss@ywesee.com # Do not require any Application-Internals in this file +$: << File.expand_path("../../src", File.dirname(__FILE__)) # Rockit redefines some StringScanner stuff, unless this is set $USING_STRSCAN = true require 'config' +require 'util/workdir' module ODDB - PROJECT_ROOT = File.expand_path('../..', File.dirname(__FILE__)) - IMAGE_DIR = File.join(PROJECT_ROOT, 'doc', 'resources', 'images') + IMAGE_DIR = File.join(WORK_DIR, 'doc', 'resources', 'images') PAYPAL_SERVER = ODDB.config.paypal_server PAYPAL_RECEIVER = ODDB.config.paypal_receiver diff --git a/src/util/workdir.rb b/src/util/workdir.rb new file mode 100644 index 000000000..c5d1748e4 --- /dev/null +++ b/src/util/workdir.rb @@ -0,0 +1,24 @@ +#!/usr/bin/env ruby +module ODDB + # Here we define some helper constants which define where we place files + # when running the application or the unittest + # Our expectation is that running all unittest only creates files under data4tests + # and that non of the checked in files is changes. This is tested in the github test runner + if defined?(Minitest) + WORK_DIR = File.expand_path('../../data4tests', File.dirname(__FILE__)) + # TEST_DATA_DIR here we find data we use for teste, eg. xml, xlsx, html or csv files + TEST_DATA_DIR = File.expand_path('../../test/data', File.dirname(__FILE__)) + else + WORK_DIR = File.expand_path('../../data', File.dirname(__FILE__)) + end + + # values differ for tests and application + EXPORT_DIR = File.join(WORK_DIR, 'downloads') + LOG_DIR = File.join(WORK_DIR, 'log') + CSV_DIR = File.join(WORK_DIR, 'csv') + XML_DIR = File.join(WORK_DIR, 'xml') + + # Same value for tests and application + PROJECT_ROOT = File.expand_path('../..', File.dirname(__FILE__)) + RESSOURCES_DIR = File.join(PROJECT_ROOT, 'doc/ressources') +end diff --git a/src/view/user/download.rb b/src/view/user/download.rb index 685bd67a0..2c2a83c29 100644 --- a/src/view/user/download.rb +++ b/src/view/user/download.rb @@ -5,6 +5,7 @@ require 'htmlgrid/passthru' require 'util/logfile' require 'plugin/yaml' +require 'util/workdir' module ODDB module View @@ -12,7 +13,7 @@ module User class Download < HtmlGrid::PassThru def init if(filename = @session.user_input(:filename)) - dir = File.expand_path('../../../data/downloads', File.dirname(__FILE__)) + dir = ODDB::EXPORT_DIR @path = File.join(dir, filename) end end diff --git a/src/view/user/export.rb b/src/view/user/export.rb index 442e925e8..0e1bcb410 100644 --- a/src/view/user/export.rb +++ b/src/view/user/export.rb @@ -7,9 +7,6 @@ module ODDB module View module User module Export - EXPORT_DIR = File.expand_path( - '../../../data/downloads', - File.dirname(__FILE__)) EXPORT_FILE = '' def default_month(filename) case filename @@ -130,14 +127,14 @@ def once_or_year(filename) end end def file_path(filename) - File.expand_path(filename, self::class::EXPORT_DIR) + File.expand_path(filename, ODDB::EXPORT_DIR) end def file_paths(filename) if uncompressed?(filename) - return [File.expand_path(filename, self::class::EXPORT_DIR)] + return [File.expand_path(filename, ODDB::EXPORT_DIR)] end ['.zip', '.gz', '.tar.gz'].collect { |suffix| - File.expand_path(filename + suffix, self::class::EXPORT_DIR) + File.expand_path(filename + suffix, ODDB::EXPORT_DIR) } end def filesize(filename) diff --git a/test/helpers.rb b/test/helpers.rb index c6b7e403e..0bb0260b3 100644 --- a/test/helpers.rb +++ b/test/helpers.rb @@ -87,7 +87,7 @@ def show_results_and_exit } diffSeconds = (Time.now - StartTime).to_i puts "#{Time.now}: OddbTestRunner::Overall result for #{@rootDir} is #{okay}" - puts "#{Time.now}: OddbTestRunner::Overall failing test_suites were #{problems.join(',')}" if problems.size > 0 + puts "#{Time.now}: OddbTestRunner::Overall failing test_suites were \n#{problems.join("\n")}" if problems.size > 0 puts " Took #{(diffSeconds/60).to_i} minutes and #{diffSeconds % 60} seconds to run" exit 2 unless okay okay diff --git a/test/test_helpers.rb b/test/test_helpers.rb index 860403703..e01cc45d5 100644 --- a/test/test_helpers.rb +++ b/test/test_helpers.rb @@ -5,6 +5,8 @@ require 'fileutils' require 'zip' require 'flexmock' +$: << File.expand_path('../src', File.dirname(__FILE__)) +require 'util/workdir' begin require 'debug'; rescue LoadError; end # ignore error when debug cannot be loaded (for Jenkins-CI) @@ -39,7 +41,6 @@ def TestHelpers.unzip_files(zipfile_name, directory) ensure Dir.chdir(savedDir) end - WorkDir = Dir.pwd LEVETIRACETAM_GTIN = 7680620690084 LEVETIRACETAM_PHAR = 5819012 LEVETIRACETAM_NAME_DE = 'LEVETIRACETAM DESITIN Mini Filmtab 250 mg 30 Stk' @@ -89,7 +90,7 @@ def TestHelpers.vcr_setup c.cassette_library_dir = File.expand_path("#{Dir.pwd}/fixtures/vcr_cassettes") c.before_record(:lppv) do |i| if /LPPV_D/.match(i.request.uri) - dummy = File.join(WorkDir, 'test', 'data', 'lppv', 'LPPV_D.xlsx') + dummy = File.join(ODDB::TEST_DATA_DIR, 'lppv', 'LPPV_D.xlsx') i.response.body = IO.read(dummy) i.response.headers['Content-Length'] = i.response.body.size puts "#{Time.now}: #{__LINE__}: URI was #{i.request.uri} replacing by #{dummy}" @@ -120,7 +121,7 @@ def TestHelpers.vcr_setup if m and true puts "#{Time.now}: SwissmedicDownloader #{m[1]} (#{i.response.body.size} bytes)." name = m[1].chomp('_') - swissmedic_dir = File.join(WorkDir, 'swissmedic') + swissmedic_dir = File.join(ODDB::TEST_DATA_DIR, 'swissmedic') FileUtils.makedirs(swissmedic_dir) xlsx_name = File.join(swissmedic_dir, name + '.xlsx') if /Packungen/i.match(xlsx_name) @@ -151,9 +152,9 @@ def TestHelpers.vcr_setup end end if i.response.headers['Content-Disposition'] and /XMLPublications.zip/.match(i.request.uri) - bag_dir = File.join(WorkDir, 'bag') - FileUtils.makedirs(WorkDir) - tmp_zip = File.join(WorkDir, 'XMLPublications.zip') + bag_dir = File.join(ODDB::TEST_DATA_DIR, 'bag') + FileUtils.makedirs(ODDB::TEST_DATA_DIR) + tmp_zip = File.join(ODDB::TEST_DATA_DIR, 'XMLPublications.zip') File.open(tmp_zip, 'wb+') { |f| f.write(i.response.body) } TestHelpers.unzip_files(tmp_zip, bag_dir) bag_tmp = File.join(bag_dir, 'Preparations.xml') diff --git a/test/test_model/fachinfo.rb b/test/test_model/fachinfo.rb index 112d64233..230a56c12 100755 --- a/test/test_model/fachinfo.rb +++ b/test/test_model/fachinfo.rb @@ -13,6 +13,8 @@ require 'model/fachinfo' require 'model/text' require 'yaml' +require 'util/workdir' + class Diffy::Diff attr_reader :tempfiles end @@ -379,7 +381,7 @@ def test_fachinfo_change_log_text_only_once_if_emtpy assert_equal(3, @doc.change_log.size) end def test_fachinfo_text_with_table - file = File.expand_path(File.join(File.dirname(__FILE__), '..', 'data', 'Cansartan-61215.yaml')) + file = File.join(ODDB::TEST_DATA_DIR, 'Cansartan-61215.yaml') fi = YAML.safe_load(File.read(file), permitted_classes: [ODDB::FachinfoDocument2001, ODDB::Text::Chapter, ODDB::Text::Format, diff --git a/test/test_model/sponsor.rb b/test/test_model/sponsor.rb index 30dcb4c0f..98847026f 100755 --- a/test/test_model/sponsor.rb +++ b/test/test_model/sponsor.rb @@ -10,6 +10,7 @@ require 'minitest/autorun' require 'flexmock/minitest' require 'model/sponsor' +require 'util/workdir' module ODDB class Sponsor @@ -31,10 +32,8 @@ class StubLogo def setup @sponsor = ODDB::Sponsor.new @app = StubApp.new - @file = File.expand_path('../data/sponsor/foo.gif', - File.dirname(__FILE__)) - @file2 = File.expand_path('../data/sponsor/bar.jpg', - File.dirname(__FILE__)) + @file = File.join(ODDB::WORK_DIR, 'data/sponsor/foo.gif') + @file2 = File.join(ODDB::WORK_DIR, 'data/sponsor/bar.jpg') end def teardown File.delete(@file) if File.exist?(@file) diff --git a/test/test_plugin/bsv_xml.rb b/test/test_plugin/bsv_xml.rb index 3c067a079..11939f7e6 100755 --- a/test/test_plugin/bsv_xml.rb +++ b/test/test_plugin/bsv_xml.rb @@ -995,13 +995,13 @@ def setup ODDB::TestHelpers.vcr_setup @url = 'http://bag.e-mediat.net/SL2007.Web.External/File.axd?file=XMLPublications.zip' ODDB.config.url_bag_sl_zip = @url - @archive = File.expand_path '../data', File.dirname(__FILE__) + @archive = ODDB::TEST_DATA_DIR @zip = File.join @archive, 'xml', 'XMLPublications.zip' @app = flexmock 'app' @plugin = BsvXmlPlugin.new @app - @test_src = File.expand_path '../data/xml/bsv_test.xml', File.dirname(__FILE__) + @test_src = File.join(TEST_DATA_DIR, 'xml/bsv_test.xml') assert(File.exist?(@test_src), "File #{@test_src} must exist?") - @test_conflict = File.expand_path '../data/xml/bsv_test_conflicted.xml', File.dirname(__FILE__) + @test_conflict = File.join(TEST_DATA_DIR, 'xml/bsv_test_conflicted.xml') assert(File.exist?(@test_conflict), "File #{@test_conflict} must exist?") @src = File.read(@test_src) @conflicted_src = File.read(@test_conflict) @@ -1556,7 +1556,7 @@ def setup_meddata_server opts={} end def setup_read_from_file name, iksnr=nil, seqNr=nil, packNr=nil - @test_file = File.expand_path "../data/xml/#{name}.xml", File.dirname(__FILE__) + @test_file = File.join(TEST_DATA_DIR, "xml/#{name}.xml") assert(File.exist?(@test_file), "File #{@test_file} must exist?") @src = File.read(@test_file) if iksnr diff --git a/test/test_plugin/comarketing.rb b/test/test_plugin/comarketing.rb index 66132953e..17e6d8554 100755 --- a/test/test_plugin/comarketing.rb +++ b/test/test_plugin/comarketing.rb @@ -25,18 +25,15 @@ def setup @app.should_receive(:delete).by_default @app.should_receive(:find).and_return(TestIksnr) @app.should_receive(:iksnr).and_return(TestIksnr) - @archive = File.expand_path('../var', File.dirname(__FILE__)) + @archive = ODDB::WORK_DIR # CoMarketing_small_010514.xlsx @latest = File.join @archive, 'xlsx', 'CoMarketing-latest.xlsx' @target = File.join @archive, 'xlsx', @@today.strftime('CoMarketing-%Y.%m.%d.xlsx') @plugin = flexmock('plugin', CoMarketingPlugin.new(@app, @archive)) - @data = File.expand_path '../data/xls/CoMarketing.xlsx', - File.dirname(__FILE__) - @older = File.expand_path '../data/xls/CoMarketing.older.xlsx', - File.dirname(__FILE__) - @initial = File.expand_path '../data/xls/CoMarketing.initial.xlsx', - File.dirname(__FILE__) + @data = File.join(ODDB::TEST_DATA_DIR, 'xls/CoMarketing.xlsx') + @older = File.join(ODDB::TEST_DATA_DIR, 'xls/CoMarketing.older.xlsx') + @initial = File.join(ODDB::TEST_DATA_DIR, 'xls/CoMarketing.initial.xlsx') end def test_report_with_test_file result = @plugin.find(TestIksnr) diff --git a/test/test_plugin/csv_export.rb b/test/test_plugin/csv_export.rb index 09e57a56e..3d3f64664 100755 --- a/test/test_plugin/csv_export.rb +++ b/test/test_plugin/csv_export.rb @@ -16,12 +16,8 @@ require 'test_plugin/plugin' require 'view/drugs/csv_result' require 'util/log' +require 'util/workdir' require 'model/galenicgroup' -test_data_dir = File.expand_path('../../data/csv', __FILE__) -ODDB::CsvExportPlugin.class_eval { remove_const(:EXPORT_DIR) } -ODDB::CsvExportPlugin.class_eval { EXPORT_DIR = test_data_dir } -ODDB::CsvExportPlugin.class_eval { remove_const(:MIGEL_EXPORT_DIR) } -ODDB::CsvExportPlugin.class_eval { MIGEL_EXPORT_DIR = test_data_dir } module ODDB class TestCsvExportPlugin nil, :cp => 'cp' ) - test_data_dir = File.expand_path('../../data/csv', __FILE__) log_group = flexmock('log_group', :newest_date => Time.local(2011,2,3)) flexmock(@app, :log_group => log_group ) export_server = flexmock('export_server', :compress => 'compress') - temporary_replace_constant(@plugin, 'ODDB::CsvExportPlugin::EXPORT_SERVER', export_server ) do - @plugin.instance_eval('@options = {}') - assert_equal('compress', @plugin.export_oddb_dat_with_migel(nil)) + export_dir = File.join(ODDB::TEST_DATA_DIR, 'csv') + puts export_dir + temporary_replace_constant(@plugin, 'ODDB::CsvExportPlugin::MIGEL_EXPORT_DIR', export_dir ) do + temporary_replace_constant(@plugin, 'ODDB::CsvExportPlugin::EXPORT_SERVER', export_server ) do + @plugin.instance_eval('@options = {}') + assert_equal('compress', @plugin.export_oddb_dat_with_migel(nil)) + end end end def test_export_teilbarkeit diff --git a/test/test_plugin/divisibility.rb b/test/test_plugin/divisibility.rb index cc6153124..e2030fc35 100755 --- a/test/test_plugin/divisibility.rb +++ b/test/test_plugin/divisibility.rb @@ -65,7 +65,7 @@ def test_update_from_csv_with_valid_path def @app.update(pointer, values, origin=nil) @system.update(pointer, values, origin) end - @plugin.update_from_csv File.expand_path('../data/csv/teilbarkeit_example.csv', File.dirname(__FILE__)) + @plugin.update_from_csv File.join(ODDB::TEST_DATA_DIR, 'csv/teilbarkeit_example.csv') assert_equal(1, @plugin.created_div) assert_equal(0, @plugin.updated_div) assert_equal(1, @plugin.updated_sequences.size) diff --git a/test/test_plugin/epha_interactions.rb b/test/test_plugin/epha_interactions.rb index d20c33d8f..459261252 100755 --- a/test/test_plugin/epha_interactions.rb +++ b/test/test_plugin/epha_interactions.rb @@ -144,14 +144,12 @@ def recount class TestEphaInteractionPlugin ['packages'], :pointer => 'pointer', @@ -165,7 +163,7 @@ def setup :creator => @sequence) seq_ptr.should_receive(:+).with([:sequence, 0]).and_return(@sequence) FileUtils.rm_f(ODDB::EphaInteractions::CSV_FILE, verbose: true) - @fileName = File.join(@@vardir, 'epha_interactions_de_utf8-example.csv') + @fileName = File.join(ODDB::WORK_DIR, 'epha_interactions_de_utf8-example.csv') @latest = @fileName.sub('.csv', '-latest.csv') @plugin = flexmock('epha_plugin', ODDB::EphaInteractionPlugin.new(@app, {})) @mock_latest = flexmock('latest', Latest) @@ -174,16 +172,14 @@ def setup end def teardown - FileUtils.rm_rf(@@vardir, :verbose => true) ODBA.storage = nil super # to clean up FlexMock end def test_update_epha_interactions_empty - FileUtils.rm_rf(Dir.glob("#{@@vardir}/*"), :verbose => false) assert(@plugin.update(@fileName)) report = @plugin.report - files = Dir.glob("#{@@vardir}/*") + files = Dir.glob("#{ODDB::WORK_DIR}/*csv") assert_equal(3, files.size) assert(report.match("EphaInteractionPlugin.update latest")) assert(report.match(/Added 1 interactions/)) @@ -193,7 +189,7 @@ def test_update_epha_interactions_update @plugin.should_receive(:fetch_with_http).with(ODDB::EphaInteractions::CSV_ORIGIN_URL).and_return('old_content') assert(@plugin.update(@fileName)) report = @plugin.report - files = Dir.glob("#{@@vardir}/*") + files = Dir.glob("#{ODDB::WORK_DIR}/*.csv") assert_equal(3, files.size) assert(report.match("EphaInteractionPlugin.update latest")) assert(report.match(/Added 1 interactions/)) diff --git a/test/test_plugin/lppv.rb b/test/test_plugin/lppv.rb index 78d92fd55..02086cad6 100755 --- a/test/test_plugin/lppv.rb +++ b/test/test_plugin/lppv.rb @@ -27,6 +27,9 @@ def each_package(&block) end end def setup + FileUtils.rm_rf(ODDB::WORK_DIR) + FileUtils.makedirs(ODDB::WORK_DIR) + FileUtils.cp(File.join(ODDB::TEST_DATA_DIR, 'lppv/LPPV_D.xlsx'), ODDB::WORK_DIR) ODDB::TestHelpers.vcr_setup package1 = flexmock('package', :barcode => '7680554950049', diff --git a/test/test_plugin/medical_products.rb b/test/test_plugin/medical_products.rb index 2fbb22622..ac7505e54 100755 --- a/test/test_plugin/medical_products.rb +++ b/test/test_plugin/medical_products.rb @@ -142,9 +142,9 @@ def recount end class TestMedicalProductPlugin nil, :pointer => 'pointer' @@ -274,8 +274,8 @@ def test_update_report_empty end def test_get_latest_file - latest = File.expand_path(File.join(__FILE__, "../../../data/xml/refdata_jur_latest.xml")) - current = File.expand_path(File.join(__FILE__, "../../../data/xml/refdata_jur_#{Time.now.strftime('%Y.%m.%d')}.xml")) + latest = File.join(ODDB::WORK_DIR, "xml/refdata_jur_latest.xml") + current = File.join(ODDB::WORK_DIR, "xml/refdata_jur_#{Time.now.strftime('%Y.%m.%d')}.xml") FileUtils.rm_f(current) if File.exist?(current) FileUtils.rm_f(latest) if File.exist?(latest) @plugin = ODDB::Companies::RefdataJurPlugin.new(@app) diff --git a/test/test_plugin/refdata_nat.rb b/test/test_plugin/refdata_nat.rb index bcd3fdde9..22f3b1f17 100755 --- a/test/test_plugin/refdata_nat.rb +++ b/test/test_plugin/refdata_nat.rb @@ -10,7 +10,7 @@ require 'tempfile' class TestRefdataNatPlugin true) diff --git a/test/test_plugin/swissmedic.rb b/test/test_plugin/swissmedic.rb index aae4ce429..65558b739 100755 --- a/test/test_plugin/swissmedic.rb +++ b/test/test_plugin/swissmedic.rb @@ -62,13 +62,13 @@ def setup ODDB::GalenicGroup.reset_oids ODBA.storage.reset_id @app = flexmock(ODDB::App.new) - @archive = File.expand_path('../var', File.dirname(__FILE__)) + @archive = ODDB::WORK_DIR FileUtils.rm_rf(@archive) FileUtils.mkdir_p(@archive) @plugin = flexmock('plugin', SwissmedicPlugin.new(@app, @archive)) - @state_2019_01_31 = File.expand_path '../data/xlsx/Packungen-2019.01.31.xlsx', File.dirname(__FILE__) - @state_2015_07_02 = File.expand_path '../data/xlsx/Packungen-2015.07.02.xlsx', File.dirname(__FILE__) - prep_from = File.expand_path('../data/xlsx/Erweiterte_Arzneimittelliste_HAM_31012019.xlsx', File.dirname(__FILE__)) + @state_2019_01_31 = File.join(ODDB::TEST_DATA_DIR, 'xlsx/Packungen-2019.01.31.xlsx') + @state_2015_07_02 = File.join(ODDB::TEST_DATA_DIR, 'xlsx/Packungen-2015.07.02.xlsx') + prep_from = File.join(ODDB::TEST_DATA_DIR, 'xlsx/Erweiterte_Arzneimittelliste_HAM_31012019.xlsx') @plugin.should_receive(:fetch_with_http).with( ODDB::SwissmedicPlugin.get_packages_url).and_return(File.open(@state_2015_07_02).read).by_default @plugin.should_receive(:fetch_with_http).with( ODDB::SwissmedicPlugin.get_preparations_url).and_return(File.open(prep_from).read).by_default @target = File.join @archive, 'xls', @@today.strftime('Packungen-%Y.%m.%d.xlsx') @@ -76,9 +76,8 @@ def setup FileUtils.makedirs(File.dirname(@latest)) unless File.exist?(File.dirname(@latest)) FileUtils.rm(@latest) if File.exist?(@latest) - @test_packages = File.expand_path('../data/xlsx/Packungen-2019.01.31.xlsx', File.dirname(__FILE__)) - latest_to = File.expand_path('../data/xls/Packungen-latest.xlsx', File.dirname(__FILE__)) - FileUtils.makedirs(File.dirname(latest_to)) + @test_packages = File.join(ODDB::TEST_DATA_DIR, 'xlsx/Packungen-2019.01.31.xlsx') + latest_to = File.join(ODDB::TEST_DATA_DIR, 'xls/Packungen-latest.xlsx') FileUtils.cp(@test_packages, latest_to, :verbose => true, :preserve => true) FileUtils.cp(prep_from, File.join(@archive, 'xls', @@today.strftime('Präparateliste-%Y.%m.%d.xlsx')), :verbose => true, :preserve => true) @@ -673,23 +672,20 @@ def test_update_swissmedic :company_name => company) @app = flexmock(@app) @app.should_receive(:resolve).and_return(nil) - newer = File.expand_path(File.join(@archive, '..', 'data', 'xlsx', 'Packungen-latest.xlsx')) + newer = File.join(ODDB::TEST_DATA_DIR, 'xlsx', 'Packungen-latest.xlsx') older = @state_2015_07_02 - FileUtils.cp(older, - File.join(@archive, 'xls', 'Packungen-latest.xlsx'), + FileUtils.cp(older, File.join(ODDB::TEST_DATA_DIR, 'xls', 'Packungen-latest.xlsx'), :verbose => true, :preserve => true) - FileUtils.cp(older, File.join(@archive, 'xls', @@today.strftime('Packungen-%Y.%m.%d.xlsx')), + FileUtils.cp(older, File.join(ODDB::TEST_DATA_DIR, 'xls', @@today.strftime('Packungen-%Y.%m.%d.xlsx')), :verbose => true, :preserve => true) result = @plugin.update - puts @plugin.report assert_equal(4, @plugin.updated_agents.size) assert_equal(15, @plugin.recreate_missing.size) assert_equal(8, @plugin.known_export_registrations.size) assert_equal(8, @plugin.known_export_sequences.size) - FileUtils.cp(newer, File.join(@archive, 'xls', @@today.strftime('Packungen-%Y.%m.%d.xlsx')), + FileUtils.cp(newer, File.join(ODDB::TEST_DATA_DIR, 'xls', @@today.strftime('Packungen-%Y.%m.%d.xlsx')), :verbose => true, :preserve => true) result = @plugin.update - puts @plugin.report assert_equal(0, @plugin.updated_agents.size) assert_equal(0, @plugin.recreate_missing.size) assert_equal(8, @plugin.known_export_registrations.size) diff --git a/test/test_plugin/swissmedic_xlsx.rb b/test/test_plugin/swissmedic_xlsx.rb index a0f500fd6..1255451ca 100755 --- a/test/test_plugin/swissmedic_xlsx.rb +++ b/test/test_plugin/swissmedic_xlsx.rb @@ -42,18 +42,18 @@ def setup ODBA.storage.reset_id mock_downloads @app = flexmock(ODDB::App.new) - @archive = File.expand_path('../var', File.dirname(__FILE__)) + @archive = ODDB::WORK_DIR FileUtils.rm_rf(@archive) FileUtils.mkdir_p(@archive) @latest = File.join @archive, 'xls', 'Packungen-latest.xlsx' - @older = File.expand_path '../data/xlsx/Packungen-2015.07.02.xlsx', File.dirname(__FILE__) + @older = File.join(ODDB::TEST_DATA_DIR, 'xlsx/Packungen-2015.07.02.xlsx') @plugin = flexmock('plugin', SwissmedicPlugin.new(@app, @archive)) - @bag_listen = File.expand_path '../data/html/listen_neu.html', File.dirname(__FILE__) - @current = File.expand_path '../data/xlsx/Packungen-2019.01.31.xlsx', File.dirname(__FILE__) + @bag_listen = File.join(ODDB::TEST_DATA_DIR, 'html/listen_neu.html') + @current = File.join(ODDB::TEST_DATA_DIR, 'xlsx/Packungen-2019.01.31.xlsx') @target = File.join @archive, 'xls', @@today.strftime('Packungen-%Y.%m.%d.xlsx') @plugin.should_receive(:fetch_with_http).with('https://www.swissmedic.ch/swissmedic/de/home/services/listen_neu.html').and_return(File.open(@bag_listen).read).by_default @plugin.should_receive(:open).with( ODDB::SwissmedicPlugin.get_packages_url).and_return(File.open(@current).read).by_default - @prep_from = File.expand_path('../data/xlsx/Erweiterte_Arzneimittelliste_HAM_31012019.xlsx', File.dirname(__FILE__)) + @prep_from = File.join(ODDB::TEST_DATA_DIR, 'xlsx/Erweiterte_Arzneimittelliste_HAM_31012019.xlsx') FileUtils.cp(@prep_from, File.join(@archive, 'xls', @@today.strftime('Erweiterte_Arzneimittelliste_HAM_31012019.xlsx')), :verbose => true, :preserve => true) FileUtils.cp(@prep_from, File.join(@archive, 'xls', 'Erweiterte_Arzneimittelliste_HAM_31012019.xlsx^'), diff --git a/test/test_plugin/swissmedic_xlsx_2021.rb b/test/test_plugin/swissmedic_xlsx_2021.rb index 93d923988..b07c3b79e 100755 --- a/test/test_plugin/swissmedic_xlsx_2021.rb +++ b/test/test_plugin/swissmedic_xlsx_2021.rb @@ -42,18 +42,18 @@ def setup ODBA.storage.reset_id mock_downloads @app = flexmock(ODDB::App.new) - @archive = File.expand_path('../var', File.dirname(__FILE__)) + @archive = ODDB::WORK_DIR FileUtils.rm_rf(@archive) FileUtils.mkdir_p(@archive) @latest = File.join @archive, 'xls', 'Packungen-latest.xlsx' - @older = File.expand_path '../data/xlsx/Packungen-2019.01.31.xlsx', File.dirname(__FILE__) + @older = File.join(ODDB::TEST_DATA_DIR, 'xlsx/Packungen-2019.01.31.xlsx') @plugin = flexmock('plugin', SwissmedicPlugin.new(@app, @archive)) - @bag_listen = File.expand_path '../data/html/listen_neu.html', File.dirname(__FILE__) - @current = File.expand_path '../data/xlsx/Packungen-2021.04.01.xlsx', File.dirname(__FILE__) + @bag_listen = File.join(ODDB::TEST_DATA_DIR, 'html/listen_neu.html') + @current = File.join(ODDB::TEST_DATA_DIR, 'xlsx/Packungen-2021.04.01.xlsx') @target = File.join @archive, 'xls', @@today.strftime('Packungen-%Y.%m.%d.xlsx') @plugin.should_receive(:fetch_with_http).with('https://www.swissmedic.ch/swissmedic/de/home/services/listen_neu.html').and_return(File.open(@bag_listen).read).by_default @plugin.should_receive(:open).with( ODDB::SwissmedicPlugin.get_packages_url).and_return(File.open(@current).read).by_default - @prep_from = File.expand_path('../data/xlsx/Erweiterte_Arzneimittelliste_HAM_31012019.xlsx', File.dirname(__FILE__)) + @prep_from = File.join(ODDB::TEST_DATA_DIR, 'xlsx/Erweiterte_Arzneimittelliste_HAM_31012019.xlsx') FileUtils.cp(@prep_from, File.join(@archive, 'xls', @@today.strftime('Erweiterte_Arzneimittelliste_HAM_31012019.xlsx')), :verbose => true, :preserve => true) @plugin.should_receive(:fetch_with_http).with( ODDB::SwissmedicPlugin.get_preparations_url).and_return(File.open(@prep_from).read).by_default diff --git a/test/test_plugin/text_info.rb b/test/test_plugin/text_info.rb index cda6bc20a..946b09d65 100755 --- a/test/test_plugin/text_info.rb +++ b/test/test_plugin/text_info.rb @@ -11,6 +11,7 @@ require 'flexmock/minitest' require 'plugin/text_info' require 'model/text' +require 'util/workdir' module ODDB class FachinfoDocument def odba_id @@ -25,14 +26,11 @@ class TextInfoPlugin end class TestTextInfoPlugin nil,) @@ -170,13 +168,14 @@ def teardown end def setup - path_check = File.expand_path(File.join(File.dirname(__FILE__), '../../etc', 'barcode_minitest.yml')) + path_check = File.join(ODDB::PROJECT_ROOT, 'etc', 'barcode_minitest.yml') assert_equal(ODDB::TextInfoPlugin::Override_file, path_check) FileUtils.rm_f(path_check, :verbose => true) pointer = flexmock 'pointer' - @aips_download = File.expand_path('../data/xml/Aips_test.xml', File.dirname(__FILE__)) - latest_from = File.expand_path('../data/xlsx/Packungen-latest.xlsx', File.dirname(__FILE__)) - latest_to = File.expand_path('../../data/xls/Packungen-latest.xlsx', File.dirname(__FILE__)) + @aips_download = File.join(ODDB::TEST_DATA_DIR, 'xml/Aips_test.xml') + latest_from = File.join(ODDB::TEST_DATA_DIR, '/xlsx/Packungen-latest.xlsx') + latest_to = File.join(ODDB::WORK_DIR, 'xls/Packungen-latest.xlsx') + FileUtils.mkdir_p(File.dirname(latest_to)) FileUtils.cp(latest_from, latest_to, :verbose => true, :preserve => true) @app = flexmock 'application' @reg = flexmock "registration_#{__LINE__}" @@ -334,7 +333,7 @@ def test_import_daily_packungen old_missing = {'680109990223_pi_de' => 'Osanit® Kügelchen', '7680109990223_pi_fr' => 'Osanit® globules', '7680109990224_pi_fr' => 'Test mit langem Namen der nicht umgebrochen sein sollte mehr als 80 Zeichen lang'} - real_override_file = File.join(File.dirname(__FILE__), '../../etc', 'barcode_to_text_info.yml') + real_override_file = File.join(ODDB::PROJECT_ROOT, 'etc', 'barcode_to_text_info.yml') assert_equal(false, File.exist?(ODDB::TextInfoPlugin::Override_file), "File #{ODDB::TextInfoPlugin::Override_file} must not exist") assert_equal(true, File.exist?(real_override_file), "File #{real_override_file} must exist") real_overrides = YAML.load(File.read(real_override_file)) diff --git a/test/test_plugin/text_info_swissmedicinfo.rb b/test/test_plugin/text_info_swissmedicinfo.rb index c6cf57ccd..48bc4df32 100755 --- a/test/test_plugin/text_info_swissmedicinfo.rb +++ b/test/test_plugin/text_info_swissmedicinfo.rb @@ -60,11 +60,6 @@ def test_odba_store if RunAll class TestTextInfoPluginAipsMetaData :fi, :reparse => false, :iksnrs => ['32917'], # auf Zeile 2477310: 1234642 2477314 :companies => [], :download => false, - :xml_file => File.join(@@datadir, 'AipsDownload.xml'), + :xml_file => File.join(ODDB::TEST_DATA_DIR, 'xml', 'AipsDownload.xml'), } @app = ODDB::App.new @parser = flexmock 'parser (simulates ext/fiparse for swissmedicinfo_xml)' - pi_path_de = File.join(@@vardir, 'html/patinfo/de/K_nzle_Passionsblume_Kapseln_swissmedicinfo.html') + pi_path_de = File.join(ODDB::TEST_DATA_DIR, 'html/patinfo/de/K_nzle_Passionsblume_Kapseln_swissmedicinfo.html') pi_de = PatinfoDocument.new - pi_path_fr = File.join(@@vardir, 'html/patinfo/fr/Capsules_PASSIFLORE__K_nzle__swissmedicinfo.html') + pi_path_fr = File.join(ODDB::TEST_DATA_DIR, 'html/patinfo/fr/Capsules_PASSIFLORE__K_nzle__swissmedicinfo.html') pi_fr = PatinfoDocument.new @parser.should_receive(:parse_patinfo_html).with(pi_path_de, :swissmedicinfo, "Künzle Passionsblume Kapseln").and_return pi_de @parser.should_receive(:parse_patinfo_html).with(pi_path_fr, :swissmedicinfo, "Capsules PASSIFLORE \"Künzle\"").and_return pi_de @@ -226,7 +212,7 @@ def setup end # Fuer Problem mit fachinfo italic def teardown - FileUtils.rm_rf @@vardir +# FileUtils.rm_rf ODDB::TEST_DATA_DIR ODBA.storage = nil super # to clean up FlexMock end @@ -244,8 +230,8 @@ def test_import_swissmedicinfo_xml pi = flexmock 'patinfo' flags = {:de => :up_to_date, :fr => :up_to_date} @parser.should_receive(:parse_textinfo).never - @parser.should_receive(:parse_fachinfo_html).at_least.once @parser.should_receive(:parse_patinfo_html).never + @parser.should_receive(:parse_fachinfo_html).at_least.once @plugin.extract_matched_content("Zyloric®", 'fi', 'de') assert(@plugin.import_swissmedicinfo(@opts), 'must be able to run import_swissmedicinfo') end @@ -259,19 +245,19 @@ def test_import_swissmedicinfo_no_iksnr # only german fachinfo is present @parser.should_receive(:parse_fachinfo_html).at_least.once @parser.should_receive(:parse_patinfo_html).never - opts = {:iksnrs => [], :xml_file => File.join(@@datadir, 'AipsDownload.xml')} + opts = {:iksnrs => [], :xml_file => File.join(ODDB::TEST_DATA_DIR, 'xml', 'AipsDownload.xml')} @plugin = TextInfoPlugin.new(@app, opts) agent = @plugin.init_agent - base = File.expand_path(File.join(__FILE__, '../../../test/data/html/swissmedic/')) + base = File.join(ODDB::TEST_DATA_DIR, 'html/swissmedic') mappings = { "http://www.swissmedicinfo.ch/Accept.aspx\?ReturnUrl=\%2f" => File.join(base, 'accept.html'), "http://www.swissmedicinfo.ch/?Lang=DE" => File.join(base, 'lang.html'), "http://www.swissmedicinfo.ch/?Lang=FR" => File.join(base, 'lang.html'), } @plugin.parser = @parser def @plugin.download_swissmedicinfo_xml - @dest = File.join(@@vardir, 'xml', 'AipsDownload_latest.xml') + @dest = File.join(ODDB::WORK_DIR, 'xml', 'AipsDownload_latest.xml') FileUtils.makedirs(File.dirname(@dest)) - FileUtils.cp(File.join(@@datadir, 'AipsDownload_xeljanz.xml'), @dest) + FileUtils.cp(File.join(ODDB::TEST_DATA_DIR, 'AipsDownload_xeljanz.xml'), @dest) File.join(@dest, 'AipsDownload_xeljanz.xml') end def @plugin.textinfo_swissmedicinfo_index @@ -297,23 +283,19 @@ def @plugin.textinfo_swissmedicinfo_index end class TestTextInfoPluginChecks :fi, :reparse => false, :iksnrs => ['32917'], # auf Zeile 2477310: 1234642 2477314 :companies => [], :download => false, - :xml_file => File.join(@@datadir, 'AipsDownload.xml'), + :xml_file => File.join(ODDB::TEST_DATA_DIR, 'xml', 'AipsDownload.xml'), } @app = flexmock('application', :update => @pointer, :delete => 'delete', @@ -323,9 +305,9 @@ def setup @app.should_receive(:registration).with(1, :swissmedicinfo, "Künzle Passionsblume Kapseln").and_return 0 @app.should_receive(:textinfo_swissmedicinfo_index) @parser = flexmock 'parser (simulates ext/fiparse for swissmedicinfo_xml)' - pi_path_de = File.join(@@vardir, 'html/patinfo/de/K_nzle_Passionsblume_Kapseln_swissmedicinfo.html') + pi_path_de = File.join(ODDB::TEST_DATA_DIR, 'html/patinfo/de/K_nzle_Passionsblume_Kapseln_swissmedicinfo.html') pi_de = PatinfoDocument.new - pi_path_fr = File.join(@@vardir, 'html/patinfo/fr/Capsules_PASSIFLORE__K_nzle__swissmedicinfo.html') + pi_path_fr = File.join(ODDB::TEST_DATA_DIR, 'html/patinfo/fr/Capsules_PASSIFLORE__K_nzle__swissmedicinfo.html') pi_fr = PatinfoDocument.new @parser.should_receive(:parse_patinfo_html).with(pi_path_de, :swissmedicinfo, "Künzle Passionsblume Kapseln").and_return pi_de @parser.should_receive(:parse_patinfo_html).with(pi_path_fr, :swissmedicinfo, "Capsules PASSIFLORE \"Künzle\"").and_return pi_de @@ -394,10 +376,6 @@ def replace_constant(constant, temp) eval "#{constant} = keep" end end - unless defined?(@@datadir) - @@datadir = File.expand_path '../data/xml', File.dirname(__FILE__) - @@vardir = File.expand_path '../var/', File.dirname(__FILE__) - end def create(dateiname, content) FileUtils.makedirs(File.dirname(dateiname)) @@ -407,21 +385,19 @@ def create(dateiname, content) end def teardown - FileUtils.rm_rf @@vardir +# TODO FileUtils.rm_rf ODDB::WORK_DIR ODBA.storage = nil super end def setup - FileUtils.mkdir_p @@vardir - ODDB.config.data_dir = @@vardir - ODDB.config.log_dir = @@vardir + FileUtils.mkdir_p ODDB::WORK_DIR @opts = { :target => :pi, :reparse => true, :iksnrs => ['43788'], :companies => [], :download => false, - :xml_file => File.join(@@datadir, '43788.xml'), + :xml_file => File.join(ODDB::TEST_DATA_DIR, 'xml', '43788.xml'), } @app = ODDB::App.new @plugin = TextInfoPlugin.new(@app, @opts) @@ -489,7 +465,7 @@ def test_import_patinfo_tramal_43788 assert(@plugin.import_swissmedicinfo(@opts), 'must be able to run import_swissmedicinfo') end assert(File.exist?(@plugin.problematic_fi_pi), "Datei #{ @plugin.problematic_fi_pi} must exist") - path = File.join(File.dirname(__FILE__), '../../doc/resources/images/pi/de/43788Tramal_Tropfen__L_sung_zum_Einnehmen_files/1.png') + path = File.join(ODDB::WORK_DIR, 'doc/resources/images/pi/de/43788Tramal_Tropfen__L_sung_zum_Einnehmen_files/1.png') assert(File.exist?(path), "Created image file #{path} must exist") @app.registration('15219').packages.size @app.registration('15219').packages.values.find_all { |x| x.patinfo} @@ -514,8 +490,8 @@ def test_import_fachinfo_tramal_43788 @opts[:target] = :fi assert(@plugin.import_swissmedicinfo(@opts), 'must be able to run import_swissmedicinfo') end - assert(File.exist?(@plugin.problematic_fi_pi)) - assert(File.size(@plugin.problematic_fi_pi) > 100) + assert(File.exist?(@plugin.problematic_fi_pi), "#{@plugin.problematic_fi_pi} must exist") + assert(File.size(@plugin.problematic_fi_pi) > 100, "#{@plugin.problematic_fi_pi} must be > 100 bytes") end end def test_import_newest_only diff --git a/test/test_plugin/who.rb b/test/test_plugin/who.rb index 3f4537c85..d83d31ecd 100755 --- a/test/test_plugin/who.rb +++ b/test/test_plugin/who.rb @@ -38,7 +38,7 @@ def teardown super end def setup - @datadir = File.expand_path '../data/html/who', File.dirname(__FILE__) + @datadir = File.join(ODDB::TEST_DATA_DIR, 'html/who') mechanize = Mechanize.new path = File.join @datadir, 'atc_ddd.html' mechanize_get = mechanize.get('file://' + path) diff --git a/test/test_util/csstemplate.rb b/test/test_util/csstemplate.rb index fb6adeba2..2799e0c5d 100755 --- a/test/test_util/csstemplate.rb +++ b/test/test_util/csstemplate.rb @@ -8,6 +8,7 @@ require 'minitest/autorun' require 'util/csstemplate' +require 'util/workdir' module ODDB class CssTemplate @@ -15,8 +16,9 @@ class CssTemplate remove_const :TEMPLATE remove_const :DEFAULT remove_const :FLAVORS - RESOURCE_PATH = "../../test/data/css/" - TEMPLATE = File.expand_path('./../data/css/template.css', File.dirname(__FILE__)) + RESOURCE_PATH = File.join(ODDB::TEST_DATA_DIR, 'css/') + TEMPLATE = File.join(ODDB::TEST_DATA_DIR, 'css/template.css') + DEFAULT = { :bar_bg => 'white', :bar_txt_color => 'black', @@ -32,7 +34,8 @@ class CssTemplate end class TestCssTemplate 'black', @@ -80,7 +83,8 @@ def test_substitute4 assert_equal(expected, ODDB::CssTemplate.substitute(src, @foo)) end def test_write_css - ODDB::CssTemplate.write_css - assert(File.exist?(TEST_CSS_DIRECTORY + '/oddb.css')) + res = ODDB::CssTemplate.write_css + tst_css = File.join(TEST_CSS_DIRECTORY, '/oddb.css') + assert(File.exist?(tst_css), "File #{tst_css} must exist") end end diff --git a/test/test_util/logfile.rb b/test/test_util/logfile.rb index 54be8dd39..c81bf23b8 100755 --- a/test/test_util/logfile.rb +++ b/test/test_util/logfile.rb @@ -8,25 +8,24 @@ require 'minitest/autorun' require 'util/logfile' +require 'util/workdir' module ODDB module LogFile - LOG_ROOT = File.expand_path('../data/log', File.dirname(__FILE__)) + LOG_ROOT = File.join(ODDB::WORK_DIR, 'log') end end class TestLogFile ['log'], @@ -89,7 +92,8 @@ def test_notify_parts part_content = "SMeX/SL-Differences (Registrations) 10.09.2014 0 SL hat anderen 5-Stelligen Swissmedic-Code als SMeX " - file = File.expand_path('../data/txt/log.txt', File.dirname(__FILE__)) + file = File.join(WORK_DIR, 'txt/log.txt') + FileUtils.mkdir_p(File.dirname(file)) File.open(file, 'w+') { |f| f.puts "Dummy content" } hash = { :recipients => ['log'], diff --git a/test/test_view/user/download.rb b/test/test_view/user/download.rb index 362e34f6c..a135c7658 100755 --- a/test/test_view/user/download.rb +++ b/test/test_view/user/download.rb @@ -10,7 +10,7 @@ require 'view/resulttemplate' require 'htmlgrid/select' require 'state/user/download' - +require 'util/workdir' module ODDB module View @@ -27,8 +27,7 @@ def setup @view = ODDB::View::User::Download.new(@model, @session) end def test_init - dir = File.expand_path('../../../data/downloads', File.dirname(__FILE__)) - assert_equal(dir + '/user_input', @view.init) + assert_equal(ODDB::EXPORT_DIR + '/user_input', @view.init) end def test_to_html flexmock(@session, diff --git a/test/test_view/user/export.rb b/test/test_view/user/export.rb index b46f34b49..108c5b47a 100755 --- a/test/test_view/user/export.rb +++ b/test/test_view/user/export.rb @@ -58,12 +58,12 @@ def test_uncompressed assert(@export.uncompressed?('filename')) end def test_file_paths - export_dir = ODDB::View::User::Export::EXPORT_DIR + export_dir = ODDB::EXPORT_DIR expected = [File.expand_path('filename', export_dir)] assert_equal(expected, @export.file_paths('filename')) end def test_file_paths__compressed - export_dir = ODDB::View::User::Export::EXPORT_DIR + export_dir = ODDB::EXPORT_DIR expected = [ File.expand_path('test.dat.zip', export_dir), File.expand_path('test.dat.gz', export_dir), @@ -72,7 +72,7 @@ def test_file_paths__compressed assert_equal(expected, @export.file_paths('test.dat')) end def test_file_path - export_dir = ODDB::View::User::Export::EXPORT_DIR + export_dir = ODDB::EXPORT_DIR expected = File.expand_path('filename', export_dir) assert_equal(expected, @export.file_path('filename')) end