如何在Ruby中为多个环境创建配置文件?

时间:2021-06-06 23:26:19

I don't want to confuse you so what I want to do is the following:

我不想让你困惑,所以我想做的是以下内容:

I have three environments:

我有三个环境:

www.env1.com
www.env2.com
www.env3.com

I want to create something to define the setup phase according the environment over which I want to run the scripts, that is:

我想根据我想要运行脚本的环境创建一些定义设置阶段的东西,即:

Current set-up:

def setup
  @verification_errors = []
  @selenium = Selenium::Client::Driver.new(
    :host => "localhost",
    :port => 4444,
    :browser => "*firefox C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe",
    :url => "www.env1.com",
    :timeout_in_second => 60
  )

  @selenium.start_new_browser_session
end

What I want:

我想要的是:

def setup
  @verification_errors = []
  @selenium = Selenium::Client::Driver.new( 
    :host => "localhost",
    :port => 4444,
    :browser => "*firefox C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe",
    **:url => This parameter configurable from a file or other source.**
    :timeout_in_second => 60
  )

  @selenium.start_new_browser_session
end

If this is possible I can switch environments without having to re-write all test cases.

如果可以的话,我可以切换环境而无需重新编写所有测试用例。

Hope you can help me out, I really need to do this.

希望你能帮助我,我真的需要这样做。

1 个解决方案

#1


3  

YAML is a great data serialization language for handling configuration information. It comes with Ruby so you only have to do:

YAML是一种用于处理配置信息的出色数据序列化语言。它附带Ruby,所以你只需要做:

require 'yaml'

to load it in, then something like:

加载它,然后像:

configuration = YAML::load_file('path/to/yamldata.yaml')

All your configuration data will be available inside the configuration variable.

所有配置数据都将在配置变量中可用。

Generally I create a stub for my YAML files by writing some Ruby code, defining the configuration hash that contains it, then telling YAML to generate the file for me. See the docs for load_file and dump for ways to do that.

通常我通过编写一些Ruby代码,定义包含它的配置哈希,然后告诉YAML为我生成文件,为我的YAML文件创建一个存根。有关执行此操作的方法,请参阅load_file和dump的文档。

For something like you're doing I'd create a hash like:

对于你正在做的事情,我会创建一个像:

configuration = {
  'env1' => "www.env1.com",
  'env2' => "www.env2.com",
  'env3' => "www.env3.com",
}

Using YAML::dump(configuration) returns:

使用YAML :: dump(配置)返回:

--- 
env1: www.env1.com
env2: www.env2.com
env3: www.env3.com

which you'd want to write to your .yaml file, then load later at run-time and access it like:

您想要写入.yaml文件,然后在运行时加载并访问它,如:

@selenium = Selenium::Client::Driver.new( 
  :host => "localhost",
  :port => 4444,
  :browser => "*firefox C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe",
  :timeout_in_second => 60
  :url => configuration['env1'],
)

You can replace 'env1' with the other keys to use env2 or env3.

您可以将'env1'替换为其他键以使用env2或env3。

Rails uses YAML to make one file handle the development, test and production information for an application. At work I use it to do similar things, where one file contains our development and production environmental information for apps, plus the definitions of some hashes we need to maintain, but don't want to have to modify the code to do so.

Rails使用YAML使一个文件处理应用程序的开发,测试和生产信息。在工作中我使用它来做类似的事情,其中​​一个文件包含我们的应用程序的开发和生产环境信息,以及我们需要维护的一些哈希的定义,但不希望必须修改代码来执行此操作。

#1


3  

YAML is a great data serialization language for handling configuration information. It comes with Ruby so you only have to do:

YAML是一种用于处理配置信息的出色数据序列化语言。它附带Ruby,所以你只需要做:

require 'yaml'

to load it in, then something like:

加载它,然后像:

configuration = YAML::load_file('path/to/yamldata.yaml')

All your configuration data will be available inside the configuration variable.

所有配置数据都将在配置变量中可用。

Generally I create a stub for my YAML files by writing some Ruby code, defining the configuration hash that contains it, then telling YAML to generate the file for me. See the docs for load_file and dump for ways to do that.

通常我通过编写一些Ruby代码,定义包含它的配置哈希,然后告诉YAML为我生成文件,为我的YAML文件创建一个存根。有关执行此操作的方法,请参阅load_file和dump的文档。

For something like you're doing I'd create a hash like:

对于你正在做的事情,我会创建一个像:

configuration = {
  'env1' => "www.env1.com",
  'env2' => "www.env2.com",
  'env3' => "www.env3.com",
}

Using YAML::dump(configuration) returns:

使用YAML :: dump(配置)返回:

--- 
env1: www.env1.com
env2: www.env2.com
env3: www.env3.com

which you'd want to write to your .yaml file, then load later at run-time and access it like:

您想要写入.yaml文件,然后在运行时加载并访问它,如:

@selenium = Selenium::Client::Driver.new( 
  :host => "localhost",
  :port => 4444,
  :browser => "*firefox C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe",
  :timeout_in_second => 60
  :url => configuration['env1'],
)

You can replace 'env1' with the other keys to use env2 or env3.

您可以将'env1'替换为其他键以使用env2或env3。

Rails uses YAML to make one file handle the development, test and production information for an application. At work I use it to do similar things, where one file contains our development and production environmental information for apps, plus the definitions of some hashes we need to maintain, but don't want to have to modify the code to do so.

Rails使用YAML使一个文件处理应用程序的开发,测试和生产信息。在工作中我使用它来做类似的事情,其中​​一个文件包含我们的应用程序的开发和生产环境信息,以及我们需要维护的一些哈希的定义,但不希望必须修改代码来执行此操作。