Skip to content

Commit

Permalink
Add print command to scripter
Browse files Browse the repository at this point in the history
  • Loading branch information
greeneca committed Jun 14, 2016
1 parent 9813e51 commit 0f1aeff
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
roku_builder (3.6.3)
roku_builder (3.6.4)
faraday (~> 0.9)
faraday-digestauth (~> 0.2)
git (~> 1.3)
Expand Down
4 changes: 4 additions & 0 deletions bin/roku
Expand Up @@ -68,6 +68,10 @@ OptionParser.new do |opts|
options[:key] = true
end

opts.on("--print ATTRIBUTE", "Command: Print attribute for scripting") do |a|
options[:print] = a
end

opts.on("--screen SCREEN", "Command: show a screen") do |s|
options[:screen] = s
end
Expand Down
4 changes: 4 additions & 0 deletions lib/roku_builder.rb
Expand Up @@ -25,6 +25,7 @@
require "roku_builder/packager"
require "roku_builder/linker"
require "roku_builder/tester"
require "roku_builder/scripter"
require "roku_builder/manifest_manager"
require "roku_builder/config_manager"
require "roku_builder/config_validator"
Expand Down Expand Up @@ -134,6 +135,9 @@ module RokuBuilder

# Did not sideload as content is identical
IDENTICAL_SIDELOAD = 13

# Bad print attribute
BAD_PRINT_ATTRIBUTE = 14
end

class ::String
Expand Down
2 changes: 1 addition & 1 deletion lib/roku_builder/controller.rb
Expand Up @@ -162,7 +162,7 @@ def self.check_devices(options:, config:, configs:, logger:)
def self.commands
[:sideload, :package, :test, :deeplink,:configure, :validate, :delete,
:navigate, :text, :build, :monitor, :update, :screencapture, :key, :screen,
:screens, :applist]
:screens, :applist, :print]
end

# List of depricated options
Expand Down
13 changes: 13 additions & 0 deletions lib/roku_builder/controller_commands.rb
Expand Up @@ -150,6 +150,19 @@ def self.deeplink(options:, configs:, logger:)
end
end

# Run Print
# @param options [Hash] user options
# @param configs [Hash] parsed configs
def self.print(options:, configs:)
stager = Stager.new(**configs[:stage_config])
code = nil
if stager.stage
code = Scripter.print(attribute: options[:print].to_sym, configs: configs)
end
stager.unstage
code
end

# Run a simple command
# @param klass [Class] class of object to create
# @param method [Symbol] methog to run on klass
Expand Down
2 changes: 2 additions & 0 deletions lib/roku_builder/error_handler.rb
Expand Up @@ -111,6 +111,8 @@ def self.handle_command_codes(command_code:, logger:)
when FAILED_SCREENCAPTURE
logger.fatal "Failed to Capture Screen"
abort
when BAD_PRINT_ATTRIBUTE
logger.fatal "Unknown print attribute"
end
end

Expand Down
37 changes: 37 additions & 0 deletions lib/roku_builder/scripter.rb
@@ -0,0 +1,37 @@
# ********** Copyright Viacom, Inc. Apache 2.0 **********

module RokuBuilder

# Helper for extending for scripting
class Scripter

# Prints attributes from config or project to allow scripting
# @param attribute [Symbol] attribute to print
# @param configs [Hash] Parsed config hash
def self.print(attribute:, configs:)
attributes = [
:title, :build_version, :app_version, :root_dir
]

unless attributes.include? attribute
return BAD_PRINT_ATTRIBUTE
end

read_config = {root_dir: configs[:project_config][:directory]}

case attribute
when :root_dir
printf "%s", configs[:project_config][:directory]
when :title
printf "%s", ManifestManager.read_manifest(**read_config)[:title]
when :build_version
printf "%s", ManifestManager.read_manifest(**read_config)[:build_version]
when :app_version
major = ManifestManager.read_manifest(**read_config)[:major_version]
minor = ManifestManager.read_manifest(**read_config)[:minor_version]
printf "%s.%s", major, minor
end
SUCCESS
end
end
end
2 changes: 1 addition & 1 deletion lib/roku_builder/version.rb
Expand Up @@ -2,5 +2,5 @@

module RokuBuilder
# Version of the RokuBuilder Gem
VERSION = "3.6.3"
VERSION = "3.6.4"
end
6 changes: 4 additions & 2 deletions tests/roku_builder/keyer_test.rb
Expand Up @@ -101,8 +101,10 @@ def test_keyer_rekey_changed
faraday.expect(:request, nil, [:url_encoded])
faraday.expect(:adapter, nil, [Faraday.default_adapter])

# This test fails with SEED=21894 due to the random number generator
# spitting out the same number twice
# This test fails with the following seeds due to the random number
# generator spitting out the same number twice
# SEED=21894
# SEED=31813
dev_id = Proc.new { Random.rand(100) }
keyer = RokuBuilder::Keyer.new(**device_config)
key_changed = nil
Expand Down
85 changes: 85 additions & 0 deletions tests/roku_builder/scripter_test.rb
@@ -0,0 +1,85 @@
# ********** Copyright Viacom, Inc. Apache 2.0 **********

require_relative "test_helper.rb"

class Scriptertest < Minitest::Test

def test_scripter_print_bad_attr
code = RokuBuilder::Scripter.print(attribute: :bad, configs: {})
assert_equal RokuBuilder::BAD_PRINT_ATTRIBUTE, code
end

def test_scripter_print_config_values
call_count = 0
code = nil
fake_print = lambda { |message, path|
assert_equal "%s", message
assert_equal "/dev/null", path
call_count+=1
}
configs = {project_config: {directory: "/dev/null"}}
RokuBuilder::Scripter.stub(:printf, fake_print) do
code = RokuBuilder::Scripter.print(attribute: :root_dir, configs: configs)
end
assert_equal 1, call_count
assert_equal RokuBuilder::SUCCESS, code
end

def test_scripter_print_manifest_title
call_count = 0
code = nil
fake_print = lambda { |message, title|
assert_equal "%s", message
assert_equal "title", title
call_count+=1
}
manifest = {title: "title"}
configs = {project_config: {directory: "/dev/null"}}
RokuBuilder::Scripter.stub(:printf, fake_print) do
RokuBuilder::ManifestManager.stub(:read_manifest, manifest) do
code = RokuBuilder::Scripter.print(attribute: :title, configs: configs)
end
end
assert_equal 1, call_count
assert_equal RokuBuilder::SUCCESS, code
end

def test_scripter_print_manifest_build_version
call_count = 0
code = nil
fake_print = lambda { |message, build|
assert_equal "%s", message
assert_equal "010101.0001", build
call_count+=1
}
manifest = {build_version: "010101.0001"}
configs = {project_config: {directory: "/dev/null"}}
RokuBuilder::Scripter.stub(:printf, fake_print) do
RokuBuilder::ManifestManager.stub(:read_manifest, manifest) do
code = RokuBuilder::Scripter.print(attribute: :build_version, configs: configs)
end
end
assert_equal 1, call_count
assert_equal RokuBuilder::SUCCESS, code
end

def test_scripter_print_manifest_app_version
call_count = 0
code = nil
fake_print = lambda { |message, major, minor|
assert_equal "%s.%s", message
assert_equal "1", major
assert_equal "0", minor
call_count+=1
}
manifest = {major_version: "1", minor_version: "0"}
configs = {project_config: {directory: "/dev/null"}}
RokuBuilder::Scripter.stub(:printf, fake_print) do
RokuBuilder::ManifestManager.stub(:read_manifest, manifest) do
code = RokuBuilder::Scripter.print(attribute: :app_version, configs: configs)
end
end
assert_equal 1, call_count
assert_equal RokuBuilder::SUCCESS, code
end
end
34 changes: 0 additions & 34 deletions tests/roku_builder/test_files/controller_test/configure_test.json

This file was deleted.

0 comments on commit 0f1aeff

Please sign in to comment.