How to use custom path in karate feature file? - pom.xml

In the karate feature file, classpath is - "scr/test/java"
But instead, the detailed "classpath:examples/resources/module1/sub_module/test.json" path, Is there any other way that hides the detailed path? like
Here, My expectation is Instead of
def file1 = read(classpath:examples/resources/module1/sub_module/test.json)
I want to use
def file1 = read(customclasspath:test.json) or similar to this which can hide the full path details of any file in the feature file.

Just use a variable?
* def myPath = 'classpath:foo/bar/'
And then later:
* def file1 = read(myPath + 'test.json')
More hints on variable substitution can be found here: https://stackoverflow.com/a/73230200/143475

Related

How to get sub content after running readFile in Jenkins

I have a question about how to get sub content after running readFile in Jenkins.
I print the content after readFile method of groovy in Jenkins
the content looks like this
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: asdfasdf
Team: {org=org1, team=team1}
ABCD: {org=org2, team=team2}
ABCDE: {org=org3, team=team3}
Bundle-Vendor: xxxxx
I just want to get this line in the content
Team: {org=org1, team=team1}
ABCD: {org=org2, team=team2}
ABCDE: {org=org3, team=team3}
but how??
any Solutions?
The content of your file looks like it is a properties file, so you can use the readProperties keyword which is part of the Pipeline Utility Steps to read the file into a map, allowing you easy access to all the values:
readProperties: Read properties from files in the workspace or text.
Reads a file in the current working directory or a String as a plain text Java Properties file. The returned object is a normal Map with String keys. The map can also be pre loaded with default values before reading/parsing the data.
You now have easy access to the keys and values.
In your case it can look like:
def props = readProperties file: 'YOUR_FILE_PATH'
// You can now access all values from the props map
def content = "Team: ${props.Team}"
If you just want the line based on parsing the file content you can read the file, split the content by new lines and use something like the following:
def lines = readFile('YOUR_FILE_PATH').split("\n").trim()
// Get a constant line
def content = lines[4]
// Get a line by prefix
def content = lines.find{ it.startsWith('Team')}

bazel: Setting a global varible from environment variable

I need to use an environment variable in my bzl file. Right now its hardcoded like this
PI_TOOLCHAIN_ROOT_DIR = "/home/dev/oosman/.leila/toolchains/rpi"
I could probably read the environment variable in the _impl method below, but how do I set the global variable above.
def _impl(ctx):
#todo: need to get this env variable and use it instead of hardcoded paths where ever rpi is defined above
#PI_TOOLCHAIN_ROOT_DIR=${HOME}/${DOCKERUSER}/.leila/toolchains/rpi
#ctx.os.environ.get("PI_TOOLCHAIN_ROOT_DIR", "/home/dev/oosman/.leila/toolchains/rpi") #osm
https://github.com/jambamamba/libedgetpu/blob/raspi0/coral_crosstool1/cc_toolchain_config.bzl
Use a repository rule to generate a .bzl file. Something like this:
def _impl(repository_ctx):
repository_ctx.file("pi_toolchain_root.bzl", "PI_TOOLCHAIN_ROOT_DIR = \"%s\"" % \
repository_ctx.os.environ.get("PI_TOOLCHAIN_ROOT_DIR", "/home/dev/oosman/.leila/toolchains/rpi"))
pi_toolchain_repository = repository_rule(
implementation=_impl,
environ = ["PI_TOOLCHAIN_ROOT_DIR"],
)
Then in your WORKSPACE you can write:
load("//:wherever.bzl", "pi_toolchain_repository")
pi_toolchain_repository(name = "pi_toolchain")
and then (later on in WORKSPACE or in a BUILD or .bzl file):
load("#pi_toolchain//:pi_toolchain_root.bzl", "PI_TOOLCHAIN_ROOT_DIR")

How to invoke CROSSTOOL tools from Bazel macros / rules?

I'm building ARM Cortex-M firmware from Bazel with a custom CROSSTOOL. I'm successfully building elf files and manually objcopying them to binary files with the usual:
path/to/my/objcopy -o binary hello.elf hello.bin
I want to make a Bazel macro or rule called cc_firmware that:
Adds the -Wl,-Map=hello.map flags to generate a mapfile
Changes the output elf name from hello to hello.elf
Invokes path/to/my/objcopy to convert the elf to a bin.
I don't know how to get the name of a CROSSTOOL tool (objcopy) to invoke it, and it feels wrong to have the rule know the path to the tool executable.
Is there a way to use the objcopy that I've already told Bazel about in my CROSSTOOL file?
You can actually access this from a custom rule. Basically you need to tell Bazel that you want access to the cpp configuration information (fragments = ["cpp"]) and then access its path via ctx.fragments.cpp.objcopy_executable, e.g.,:
def _impl(ctx):
print("path: {}".format(ctx.fragments.cpp.objcopy_executable))
# TODO: actually do something with the path...
cc_firmware = rule(
implementation = _impl,
fragments = ["cpp"],
attrs = {
"src" : attr.label(allow_single_file = True),
"map" : attr.label(allow_single_file = True),
},
outputs = {"elf" : "%{name}.elf"}
)
Then create the output you want with something like (untested):
def _impl(ctx):
src = ctx.attr.src.files.to_list()[0]
m = ctx.attr.map.files.to_list()[0]
ctx.action(
command = "{objcopy} -Wl,-Map={map} -o binary {elf_out} {cc_bin}".format(
objcopy=ctx.fragments.cpp.objcopy_executable,
map=m,
elf_out=ctx.outputs.elf.path,
cc_bin=src,
),
outputs = [ctx.outputs.elf],
inputs = [src, m],
)

Writing a file to a specific path in ruby taking the file name from excel

I am having some trouble writing a file to a specific path taking the file name from excel. Here is the code which I am using
out_file = File.new (#temp_path/ "#{obj_info[3].to_s}","w")
"#{obj_info[3].to_s}" = sample.txt
The value sample.txt comes from Excel during run time
#temp_path = "C:/Users/Somefolder/"
The error displayed is:
NoMethodError: undefined method `[]' for nil:NilClass
However, if the code is:
out_file = File.new ("#{obj_info[3].to_s}","w")
it successfully creates a file called sample.txt in the default directory. However, I want it to be stored in a specific directory and the file name needs to be passed from Excel.
Any help is much appreciated.
I believe your problem is because there a space between / and "
#temp_path/ "#{obj_info[3].to_s}
and I guess you want to build a path.
My advice is that you use File.join
f_path = File.join(#temp_path,obj_info[3].to_s)
out_file = File.new (f_path,"w")
Let me know if that solved the problem
You have 2 problems:
obj_info is nil so you make an error reading the value from excel, the error you get indicates it is on an array, in the code you published the only thing that's an array is what you read from excel.
Print the contents with p obj_info right before your code to check.
#temp_path and {obj_info[3].to_s} need to be concatenated to make a path.
You can do that with File.join like Mauricio suggests or like this
out_file = File.new ("#{#temp_path}/#{obj_info[3]}","w")
You can drop the to_s in this case.
It would be better if you publish the whole of your script that is meaningful.

Rails 3.1 absolute URL to an image

I'm using Rails 3.1. I'm trying to figure this out, and to my surprise, it is starting to seem that rails does not come with this method at all. Maybe im wrong.
Can anyone show how I can get a full absolute URL to an image?
I use asset_path(image.png) which gives me the relative path to use within the app. I tried doing a root_url + asset_path(image.png) but that just gives me a http://localhost:3000//assets/image.png with the double slashes
Anyone have an efficient way of doing this?
See the Using asset hosts section in the documentation. You need to specify an asset_host. You can also construct it dynamically from the request chaining "#{request.protocol}#{request.host_with_port}"
put this in application_helper.rb
def asset_url asset
"#{request.protocol}#{request.host_with_port}#{asset_path(asset)}"
end
then you can use asset_url in your views.
For Rails 4, and maybe earlier, use:
config.action_mailer.asset_host = 'https://assets.com'
per https://github.com/fphilipe/premailer-rails/issues/16
In my config/environments/*.rb I already have this tailored for each environment:
config.domain = 'mysite.dev'
So it was a simple matter of adding
config.action_controller.asset_host = "http://" + config.domain
to each file. Then asset_path will miraculously behave as if it were asset_url.
Example folder structure.
app/
assets/
flags/
32x32/
en.png
256x256/
en.png
If you want to generate absolute flag image path we can add in to our ApplicationHelper two methods:
module ApplicationHelper
# Generate flag path by locale
# - locale. Can be "en", "it", etc.
# - flag_size. Will be used to set folder size. Folder size can be "32x32", "256x256".
# Return flag image path. Path will absolute
def generate_flag_path_by_locale(locale, folder_size = "32")
folder = "#{flag_size}x#{flag_size}"
domain_absolute_path = generate_domain_absolute_path
flag_path = ("#{domain_absolute_path}/assets/flags/#{folder}/#{locale}.png")
return flag_path
end
# Generate domain absolute path
def generate_domain_absolute_path
request_protocol = request.protocol
request_host_with_port = request.host_with_port
domain_absolute_path = request_protocol + request_host_with_port
return domain_absolute_path
end
end
Into our apps/view/products.html.erb. We must to call only:
<% flag_path = generate_flag_path_by_locale("en") %>
Final result:
http://my_domain.com:3000/assets/flags/32x32/en.png
Could you just do:
root_url[0..-2] + asset_path(image.png)
...to trim the trailing slash in the root url?
You need to use 'asset_url' instead *asset_path*.
Bcz '_path' always return relative path and '_url' will return absolute url.

Resources