Skip to content

Commit 8204840

Browse files
committed
Fixing Android unit tests
We need to pass in directories properly since it isn't run from the app directory like we expected.
1 parent 08633f1 commit 8204840

3 files changed

Lines changed: 24 additions & 10 deletions

File tree

lib/oscli.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ def execute
5252
xcodeproj_path = Dir.pwd + '/' + entrypoint + '.xcodeproj'
5353
ios_proj.install_onesignal!(xcodeproj_path)
5454
elsif type_downcase == 'android'
55-
OSProject::GoogleAndroid.new(entrypoint, appid).add_sdk!()
55+
dir = Dir.pwd
56+
OSProject::GoogleAndroid.new(entrypoint, appid, dir).add_sdk!()
5657
else
5758
puts 'Invalid type (ios or android)'
5859
error_track_message = "User provide invalid type: #{type}"

lib/osproject_android.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
class OSProject::GoogleAndroid < OSProject
66
attr_accessor :app_class_location
7+
attr_accessor :dir
78

8-
def initialize(app_class_location, os_app_id)
9+
def initialize(app_class_location, os_app_id, dir)
910
@app_class_location = app_class_location
10-
11+
@dir = dir
1112
directory_split = app_class_location.split('/', -1)
1213
lang_array = directory_split[-1].split(".")
1314

@@ -19,7 +20,6 @@ def initialize(app_class_location, os_app_id)
1920
end
2021

2122
lang = lang_array[1]
22-
dir = Dir.pwd
2323

2424
unless lang == "java" || lang == "kt"
2525
puts 'Invalid language (java or kotlin)'
@@ -58,7 +58,6 @@ def add_sdk!
5858
lang_index = (app_directory_split.index "main") + 2
5959
# Get Package directory in the form of ex: com.orgname.appname
6060
package_directory = app_directory_split.slice(lang_index..-2).join(".")
61-
6261
puts "Application class will be created under #{dir}/#{app_class_location}\nThis is needed for SDK init code. By saying (N) you can setup the init code by following https://documentation.onesignal.com/docs/android-sdk-setup#step-3-add-required-code.\nProceed with creation? (Y/N)"
6362
user_response = STDIN.gets.chomp.downcase
6463

@@ -293,6 +292,7 @@ def add_application_init_code(project_dir, app_class_location, lang)
293292

294293
def has_sdk?
295294
# TODO: more robust testing
296-
return File.readlines(dir + '/app/build.gradle').grep(/OneSignal/).any?
295+
return File.readlines(dir + '/build.gradle').grep(/onesignal/).any? &&
296+
File.readlines(dir + '/app/build.gradle').grep(/onesignal/).any?
297297
end
298298
end

spec/osproject_spec.rb

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,37 @@
2323
FileUtils.cp_r(sampledir, projdir, verbose: true)
2424
end
2525
after(:all) do
26-
#FileUtils.remove_entry tmpdir
26+
FileUtils.remove_entry tmpdir
2727
end
2828
context sampledirname do
2929
it "successfully instantitates the object" do
3030
if platform == 'googleandroid'
31-
proj = proj_class.new(projdir, 'app_id')
31+
langextension =
32+
if lang == 'kotlin'
33+
'kt'
34+
else
35+
lang
36+
end
37+
appclasspath = 'app/src/main/'+lang+'/com/onesignal/spec/samples/'+lang+'_bottom_nav/ApplicationClass.'+langextension
38+
proj = proj_class.new(appclasspath, 'app_id', projdir)
3239
elsif platform =='iOS'
3340
proj = proj_class.new(projdir, 'targetname', lang, 'app_id')
3441
expect(proj.type.to_s).to eq platform
3542
expect(proj.dir).to eq projdir
3643
end
3744
end
3845
it "successfully adds sdk" do
39-
4046
if platform == 'googleandroid'
47+
langextension =
48+
if lang == 'kotlin'
49+
'kt'
50+
else
51+
lang
52+
end
4153
# For Android samples, we have a appclassfile symlink in the proj root dir
4254
# When users use the CLI, they specify the file.
43-
proj = proj_class.new(projdir, 'app_id')
55+
appclasspath = 'app/src/main/java/com/onesignal/spec/samples/'+lang+'_bottom_nav/ApplicationClass.'+langextension
56+
proj = proj_class.new(appclasspath, 'app_id', projdir)
4457
expect(proj.has_sdk?()).to eq false
4558
proj.add_sdk!()
4659
expect(proj.has_sdk?()).to eq true

0 commit comments

Comments
 (0)