Skip to content

Commit 222cd47

Browse files
authored
Allow to name toml files differently than appium.txt, fixes #280 (#397)
* Allow to name toml files differently than appium.txt, fixes #280 * rename load_appium_txt to load_settings and split it in 2 methods * rubocop rule violation fixed
1 parent d3a9235 commit 222cd47

File tree

4 files changed

+49
-45
lines changed

4 files changed

+49
-45
lines changed

android_tests/lib/android/specs/driver.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ def sauce?
66
ENV['UPLOAD_FILE'] && ENV['SAUCE_USERNAME']
77
end
88

9-
t 'load_appium_txt' do
10-
appium_txt = File.expand_path(File.join(Dir.pwd, 'lib'))
11-
parsed = Appium.load_appium_txt file: appium_txt, verbose: true
9+
t 'load_settings' do
10+
appium_txt = File.join(Dir.pwd, 'appium.txt')
11+
parsed = Appium.load_settings file: appium_txt, verbose: true
1212
apk_name = File.basename parsed[:caps][:app]
1313
assert_equal apk_name, 'api.apk'
1414
end

android_tests/lib/run.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ def start_driver(caps)
2121
a = OpenStruct.new x: 'ok'
2222
fail 'x issue' unless a.x == 'ok'
2323

24-
appium_txt = File.expand_path(File.join(Dir.pwd, 'lib'))
25-
dir = appium_txt
24+
dir = File.expand_path(File.join(Dir.pwd, 'lib'))
25+
appium_txt = File.join(Dir.pwd, 'appium.txt')
2626
device = ARGV[0].downcase.strip
2727
devices = %w(android selendroid ios)
2828
fail 'Expected android, selendroid or ios as first argument' unless devices.include? device
2929

3030
one_test = ARGV[1]
3131
test_dir = "/#{device}/"
3232

33-
caps = Appium.load_appium_txt file: appium_txt, verbose: true
33+
caps = Appium.load_settings file: appium_txt, verbose: true
3434
caps = caps.merge(appium_lib: { debug: true, wait: 1 })
3535
caps[:app] = ENV['SAUCE_PATH'] if ENV['SAUCE_USERNAME'] && ENV['SAUCE_ACCESS_KEY']
3636

ios_tests/lib/ios/specs/driver.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ def sauce?
1717
data.strip.must_equal 174.chr('UTF-8')
1818
end
1919

20-
t 'load_appium_txt' do
20+
t 'load_settings' do
2121
# skip this test if we're using Sauce
2222
# the storage API doesn't have an on disk file
2323
skip if sauce?
24-
appium_txt = File.expand_path(File.join(Dir.pwd, 'lib'))
25-
opts = Appium.load_appium_txt file: appium_txt, verbose: true
24+
appium_txt = File.join(Dir.pwd, 'appium.txt')
25+
opts = Appium.load_settings file: appium_txt, verbose: true
2626

2727
actual = ''
2828
actual = File.basename opts[:caps][:app] if opts && opts[:caps]

lib/appium_lib/driver.rb

+40-36
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ class Spec < Test
5151

5252
module Appium
5353
# Load appium.txt (toml format)
54-
# the basedir of this file + appium.txt is what's used
5554
#
5655
# ```
5756
# [caps]
@@ -67,17 +66,15 @@ module Appium
6766
#
6867
# @param opts [Hash] file: '/path/to/appium.txt', verbose: true
6968
# @return [hash] the symbolized hash with updated :app and :require keys
70-
def self.load_appium_txt(opts = {})
69+
def self.load_settings(opts = {})
7170
fail 'opts must be a hash' unless opts.is_a? Hash
7271
fail 'opts must not be empty' if opts.empty?
7372

74-
file = opts[:file]
75-
fail 'Must pass file' unless file
73+
toml = opts[:file]
74+
fail 'Must pass file' unless toml
7675
verbose = opts.fetch :verbose, false
7776

78-
parent_dir = File.dirname file
79-
toml = File.expand_path File.join parent_dir, 'appium.txt'
80-
Appium::Logger.info "appium.txt path: #{toml}" if verbose
77+
Appium::Logger.info "appium settings path: #{toml}" if verbose
8178

8279
toml_exists = File.exist? toml
8380
Appium::Logger.info "Exists? #{toml_exists}" if verbose
@@ -93,40 +90,47 @@ def self.load_appium_txt(opts = {})
9390
data[:caps][:app] = Appium::Driver.absolute_app_path data
9491
end
9592

96-
# return list of require files as an array
97-
# nil if require doesn't exist
9893
if data && data[:appium_lib] && data[:appium_lib][:require]
99-
r = data[:appium_lib][:require]
100-
r = r.is_a?(Array) ? r : [r]
101-
# ensure files are absolute
102-
r.map! do |f|
103-
file = File.exist?(f) ? f : File.join(parent_dir, f)
104-
file = File.expand_path file
105-
106-
File.exist?(file) ? file : nil
107-
end
108-
r.compact! # remove nils
94+
parent_dir = File.dirname toml
95+
data[:appium_lib][:require] = expand_required_files(parent_dir, data[:appium_lib][:require])
96+
end
10997

110-
files = []
98+
data
99+
end
111100

112-
# now expand dirs
113-
r.each do |item|
114-
unless File.directory? item
115-
# save file
116-
files << item
117-
next # only look inside folders
118-
end
119-
Dir.glob(File.expand_path(File.join(item, '**', '*.rb'))) do |f|
120-
# do not add folders to the file list
121-
files << File.expand_path(f) unless File.directory? f
122-
end
123-
end
101+
class << self
102+
alias_method :load_appium_txt, :load_settings
103+
end
124104

125-
# Must not sort files. File order is specified in appium.txt
126-
data[:appium_lib][:require] = files
105+
# @param base_dir [String] parent directory of loaded appium.txt (toml)
106+
# @param file_paths
107+
# @return list of require files as an array, nil if require doesn't exist
108+
def self.expand_required_files(base_dir, file_paths)
109+
# ensure files are absolute
110+
Array(file_paths).map! do |f|
111+
file = File.exist?(f) ? f : File.join(base_dir, f)
112+
file = File.expand_path file
113+
114+
File.exist?(file) ? file : nil
127115
end
116+
r.compact! # remove nils
128117

129-
data
118+
files = []
119+
120+
# now expand dirs
121+
file_paths.each do |item|
122+
unless File.directory? item
123+
# save file
124+
files << item
125+
next # only look inside folders
126+
end
127+
Dir.glob(File.expand_path(File.join(item, '**', '*.rb'))) do |f|
128+
# do not add folders to the file list
129+
files << File.expand_path(f) unless File.directory? f
130+
end
131+
end
132+
133+
files
130134
end
131135

132136
# convert all keys (including nested) to symbols
@@ -430,7 +434,7 @@ def self.absolute_app_path(opts)
430434
return app_path unless app_path.match(/[\/\\]/)
431435

432436
# relative path that must be expanded.
433-
# absolute_app_path is called from load_appium_txt
437+
# absolute_app_path is called from load_settings
434438
# and the txt file path is the base of the app path in that case.
435439
app_path = File.expand_path app_path
436440
fail "App doesn't exist #{app_path}" unless File.exist? app_path

0 commit comments

Comments
 (0)