Class: ConfigFile

Inherits:
Object
  • Object
show all
Defined in:
lib/trail_marker/config_file.rb

Overview

History:

2/19/2018 Created [email protected]

Direct Known Subclasses

TrailMarker::ConfigFile

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cfile) ⇒ ConfigFile

Returns a new instance of ConfigFile.



11
12
13
14
# File 'lib/trail_marker/config_file.rb', line 11

def initialize(cfile)
  @filename = cfile
  read_config_file
end

Instance Attribute Details

#default_commentObject

Returns the value of attribute default_comment.



9
10
11
# File 'lib/trail_marker/config_file.rb', line 9

def default_comment
  @default_comment
end

#filenameObject

Returns the value of attribute filename.



9
10
11
# File 'lib/trail_marker/config_file.rb', line 9

def filename
  @filename
end

#testrail_urlObject

Returns the value of attribute testrail_url.



9
10
11
# File 'lib/trail_marker/config_file.rb', line 9

def testrail_url
  @testrail_url
end

#tokenObject

Returns the value of attribute token.



9
10
11
# File 'lib/trail_marker/config_file.rb', line 9

def token
  @token
end

#usernameObject

Returns the value of attribute username.



9
10
11
# File 'lib/trail_marker/config_file.rb', line 9

def username
  @username
end

Instance Method Details

#check_create_configfileObject



59
60
61
62
63
64
65
66
# File 'lib/trail_marker/config_file.rb', line 59

def check_create_configfile
  if ! File.exist?(@filename)
    puts "\nWARNING: Configuration File does not exist."
    if user_continue?("Create a new config file (Y/N)? : ", 'N')
      create_configfile
    end
  end
end

#config_projectObject



88
89
90
91
92
# File 'lib/trail_marker/config_file.rb', line 88

def config_project
  @project_name = prompt_user("Enter testrail project name: ")
  @testrun = prompt_user("Enter name of test run: ", "NA")
  @testplan = prompt_user("Enter name of test plan: ", "NA")
end

#create_configfileObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/trail_marker/config_file.rb', line 68

def create_configfile
  @username = prompt_user("Enter testrail email (#{@username}): ", @username)
  @token = prompt_user("Enter testrail token (#{@token}): ", @token)
  @testrail_url = prompt_user("TestRail URL (#{@testrail_url}): ", @testrail_url)
  @default_comment = prompt_user("Test comment (Default - Marked by Automation): ", "Marked by Automation")
  @trail_info = Hash.new
  @trail_info["username"] = @username
  @trail_info["token"] = @token
  @trail_info["testrail_url"] = @testrail_url
  @trail_info["default_comment"] = @default_comment
  save
  # Comment out until overwrite is finished.
  #if user_continue?("Do you want to save the testrail project info (Y/N)?")
  #  config_project
  #  thash["project_name"] = @project_name
  #  thash["testrun"] = @testrun
  #  thash["testplan"] = @testplan
  #end
end

#prompt_user(user_msg, default_val) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/trail_marker/config_file.rb', line 16

def prompt_user(user_msg, default_val)
  retval = default_val
  newval = ''
  atmps = 0
  while newval.strip == '' && atmps < 3
    atmps += 1
    print "#{user_msg}"
    newval = STDIN.gets
    retval = newval.strip == '' ? default_val : newval.strip
    newval = retval
  end
  if atmps >= 3
    puts "\nERROR: Value cannot be empty... exiting."
    exit(0)
  end
  return retval
end

#read_config_fileObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/trail_marker/config_file.rb', line 43

def read_config_file
  if File.exist?(@filename)
    @trail_info = YAML.load_file(@filename)
    @username = @trail_info["username"]
    @token = @trail_info["token"]
    @testrail_url = @trail_info["testrail_url"]
    @default_comment = @trail_info['default_comment']
  else
    @trail_info = Hash.new
    @username = ''
    @token = ''
    @testrail_url = ''
    @default_comment = 'Marked by Automation'
  end
end

#saveObject



100
101
102
103
104
105
# File 'lib/trail_marker/config_file.rb', line 100

def save
  File.open(@filename, 'w') { |f|
    f.write @trail_info.to_yaml
    puts "Successfully saved config file: #{@filename}"
  }
end

#update(varname, varvalue) ⇒ Object



94
95
96
97
98
# File 'lib/trail_marker/config_file.rb', line 94

def update(varname, varvalue)
  if defined? @trail_info
    @trail_info[varname] = varvalue
  end
end

#user_continue?(user_msg, default_val = false) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
# File 'lib/trail_marker/config_file.rb', line 34

def user_continue?(user_msg, default_val=false)
  retval = default_val
  reply = prompt_user(user_msg, 'N')
  if reply.upcase == "Y"
    retval = true
  end
  return retval
end