I have used gflags in my test to define custom flags. How can I pass such a flag to my test while running the test via bazel test command?
For example: I can run a test multiple times using:
bazel test //xyz:my_test --runs_per_test 10
In the same command I would like to pass a flag defined in my_test say --use_xxx, how do I do so?
Use the --test_arg flag.
bazel test //xyz:my_test --runs_per_test=10 --test_arg=--use_xxx --test_arg=--some_number=42
From the docs:
--test_arg arg: Passes command-line options/flags/arguments to each test process. This
option can be used multiple times to pass several arguments, e.g.
--test_arg=--logtostderr --test_arg=--v=3.
You can also specify arguments for the test as part of the BUILD definition:
cc_test(
name = "my_test",
srcs = [".."],
deps = [".."],
args = ["--use_xxx", "--some_number=42"],
)
You can add a main into your test. It will look like this.
TEST(A, FUNC) {
// Your test here.
}
int main(int argc, char** argv) {
gflags::ParseCommandLineFlags(&argc, &argv, /*remove_flags=*/true);
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
It works fine for me.
Related
I have a single file Dart program - let's say main.dart. I'm trying to provide some compile time environment values to it using --dart-define=env=env_value but in the Dart program, I'm always getting the default values.
This is what my Dart program looks like
void main() {
const myValue = const String.fromEnvironment("MY_VALUE", defaultValue: "DEFAULT");
print('My value: $myValue'); // Always prints "DEFAULT"
}
This is the command I'm using to run my program
dart main.dart --dart-define=MY_VALUE=SOME_VALUE
Now, when I include the exact same code from above in a Flutter app and run it with the below command, everything seems to work as expecetd but for some reason the above program always prints DEFAULT as the output on console.
flutter run --dart-define=MY_VALUE=SOME_VALUE
Is there something I'm missing when it comes to providing these values in a Dart program? I'm running macOS if that helps in any way.
If you type:
dart --help --verbose
It will give you the list of supported flags.
Usage: dart [<vm-flags>] <dart-script-file> [<script-arguments>]
Executes the Dart script <dart-script-file> with the given list of <script-arguments>.
Supported options:
...
--define=<key>=<value> or -D<key>=<value>
Define an environment declaration. To specify multiple declarations,
use multiple instances of this option.
...
So it appears that the flag you want is --define or -D, rather than --dart-define. Also note that this is considered a "vm-flag" and must come before the file name in order to work.
Therefore the following command should work:
dart --define=MY_VALUE=SOME_VALUE main.dart
Before I use bazel, I always need the following code to start all the test.
::testing::GTEST_FLAG(output) = "xml";
::testing::GTEST_FLAG(output) = "xml:./test_detail.xml"
::testing::InitGoogleTest(&argc, argv);
int iRet = RUN_ALL_TESTS();
But cc_test and bazel test seems don't need this, It only need the test code.
So what cc_test and bazel test ready does? How should I configure gtest now?
It's possible that your cc_test() rules depend on #com_google_googletest//:gtest_main (source), which defines the main() function. If that doesn't do what you want, you can replace #com_google_googletest//:gtest_main with a reference to your own cc_library() that does, or by adding a main() function to every *_test.cc file.
I'm using bazel to build my android project. I need to access an environment variable DEBUG(self-defined) to determine what value of BuildConfig.DEBUG should be, but I can't find any description about this in Bazel's doc. Does Bazel support this? Or what can I do to reach my intent?
Thanks very much for any help!
PS: I'm using the genrule rule to generate my BuildConfig.java, but the value of BuildConfig.DEBUG should be determined by the environment variable DEBUG:
genrule(
name = "build-config-genrule",
outs = [ "BuildConfig.java" ],
cmd = "echo 'package com.qzone;" +
"public class BuildConfig {" +
"public static final boolean DEBUG = ???;" +
"}' > $(#)"
)
You can use the $(COMPILATION_MODE) Make Variable in the genrule.cmd:
COMPILATION_MODE: "fastbuild", "dbg", or "opt".
See Make Variable substitution.
EDIT: Important to mention that COMPILATION_MODE reflects the value of the -c / --compilation_mode flag, but there's no way in general to specify values on the command line that you could access in genrule.cmd.
I am using a custom build command to run the nasm assembler on a .asm file in my C++ project. I am using %idefs in the assembler code to only compile the code I need. I am checking for the same #defines as in the C++-Code and use define() in Premake 5 to set those, but additionally I need to pass them to nasm on its command line invocation in my Custom Build Command. What I am looking for is a way to concatenate or string replace the Premake internal list of #defines into the command line invocation string of the buildcommands() call. Is there a Premake Token or a way to introspect the lua variables and generate a list from that?
Note that my command line invocation specifically is
buildcommands "nasm.exe -f win32 -o %{cfg.objdir}%{file.basename}.lib %{file.abspath} -DNDEBUG"
Suppose I set defines { "FEAT_A", "FEAT_B" } in my premake5.lua. I then would like to to add -DFEAT_A -DFEAT_B automatically to that build command similar to the -DNDEBUG so I cannot simply insert a simple token. I guess I do have to do something like this (lua pseudo code as I don't really know the syntax):
define_flags = wks.defines.join(" -D")
buildcoommands("nasm.exe [...]"..define_flags)
Do you know if something like this is possible?
How about something like this?
buildcommands('nasm.exe [...] %{table.implode(cfg.defines, "-D", "", " ")} [...]')
I have two tasks,task_1 should compress png files and task_2 should not compress png files,so i want to add an parameter to control it.
project.ext.set("compressPngs", 1);
task taskCompressPngs(type:Exec){
commandLine "myshell.sh"
args compressPngs
}
task task_1(dependsOn:'taskCompressPngs'){}
task task_2(dependsOn:'taskCompressPngs'){}
gradle.taskGraph.whenReady { taskGraph ->
if (taskGraph.hasTask(task_1))
{
compressPngs=1
}
if (taskGraph.hasTask(task_2))
{
compressPngs=0
}
}
But when i run task_1 or task_2,in task 'taskCompressPngs', 'compressPngs' passed to my script 'myshell.sh' always be 1, why? how to solve it?
taskCompressPngs gets configured before the configuration value is changed. Conditional configuration is rarely a good solution. A better approach is to declare two Exec tasks.
As others have mentioned, it's probably best to take the advice of #PeterNiederwieser and use two separate tasks, but if you really don't think you can, here are a couple other options that should work.
1) Check Gradle startParameter
Configure your reusable task based on which task is passed to gradle on the command line.
task taskCompressPngs(type: Exec) {
def compressPngs = 1
if(gradle.startParameter.taskNames.toString().toLowerCase().contains("task_2")) compressPngs = 0
commandLine "myshell.sh $compressPngs".tokenize()
}
This gives you a variable to use (gradle.startParameter.taskNames) that is available at configuration-time.
Here we change compressPngs to 0 only if task_2 is specified on the command line when running gradle.
I.E. gradlew task_1 will run myshell.sh 1, but gradlew task_2 (or even gradlew task_1 task_2) will run myshell.sh 0.
This logic could also be applied to a project property outside of the taskCompressPngs task - if, for example, you wanted to change other tasks too.
Again, this only works if "task_2" is specified in the command used to run gradle.
2) Use DefaultExecAction instead of Exec task
Instead of using a task of type Exec, you could write a custom task and check the taskGraph in it.
task taskCompressPngs << {
def compressPngs = 1
if(gradle.taskGraph.hasTask(two)) compressPngs = 2
org.gradle.process.internal.DefaultExecAction e = new org.gradle.process.internal.DefaultExecAction(getServices().get(org.gradle.api.internal.file.FileResolver.class))
e.commandLine("myshell.sh $compressPngs".tokenize())
e.execute()
}
This is just moves your existing logic from configuration-time to execution-time.
This requires the use of "internal" Gradle classes (which is bad), but it gives you a little more flexibility in how/when the shell command is run.
Note that these solutions were checked against Gradle 1.7 and Gradle 1.11.