What does `cc_test` and `bazel test` really do? - bazel

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.

Related

dart-define not working when running a standalone Dart program

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

How to pass custom flags to `bazel test` command

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.

why main() return value does not set exitcode in dart?

Dart is suppose to be pragmatic, intuitive, etc.
I wonder Why top-level main() does not set exitcode if int is returned?
I know, you can set it via dart:io.exitCode= or use dart:io.exit().
The question is why language designers decided to drop such popular convention?
#!/path/to/dart --checked
int main() {
int myExitCode = 5;
return myExitCode;
}
It returns 0 (as described in documentation), but in command-line world it's just silly.
It does not even warn you (compiler checked mode, dartanalyzer). Script just silently return 0. Is there any rationale behind it?
UPDATE:
proposal bug: https://code.google.com/p/dart/issues/detail?id=21639
The main() method in Dart always returns void, even if you specify otherwise. See The main() function As mentioned in the answer from John Evans, you must use the exit function if you want to return a value. This is because the Dart VM runs in both CLI and in a browser. It makes no sense in terms of the browser to provide a return value from main(). Thus the functionality is limited to the dart:io library which will only run in the CLI version of the dart VM.
My understanding for this is that while main() may finish executing, you might have other asynchronous work going on that hasn't completed yet. You can use exit from the dart:io lib to return a exit code immediately.
import 'dart:io';
main(){
exit(42);
}
More info from the API docs about exit: https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/dart:io#id_exit
Possible answer would be that returning value from main() will not make sense in browser (as Matt B pointed out, +1), so dart does not let it at all.
But this is incoherent, since getting list of commandline arguments main(List<String> args) does not make sense in a browser environment either, and dart languages supports main(List<String> args).
On the other hand, support for main-return-sets-exitCode feature would tight heart of dart
(how top-level main() is treated) to dart:io which is not available in browsers.
However, I personally don't think it wouldn't be that bad as having such wired and unexpected behavior as shown in the question's code.

DART string constant set to timestamp as at compilation

How can I get a string constant automatically set to the datestamp as at compile time?
Something like:
const String COMPILE_DATESTAMP = eval_static(DateTime.now().toString());
...
String s = "This program was compiled $COMPILE_DATESTAMP";
where s would then be for e.g.
"This program was compiled 1971-02-03 04:05:06"
Thanks for the question!
There's no required compile step in Dart. (We do have an optional Dart-to-JavaScript compiler, or even a Dart-to-Dart processor that does tree shaking.) Dart's VM accepts input as text files. Similar to Ruby or Python, it runs text-based scripts.
As others have mentioned, this is a job for some sort of build step.
I'm new to Dart, but I haven't seen anything in the documentation to suggest that such a thing is possible. I strongly suspect that it isn't.
If you really need functionality like you describe, I think your best bet is to roll your own build script. Something simple like:
#!/bin/bash
sed -ri "s/INSERT_DATETIME_HERE/`date`/" $1
dart2js $1 -o$1.js
could be modified to suit your needs. (I'd want some sanity checks in there if it were me; I'm just suggesting a starting point.) Your code would become:
const String COMPILE_DATESTAMP = "INSERT_DATETIME_HERE";
...
String s = "This program was compiled $COMPILE_DATESTAMP";
You must write another dart program that could examine the actual compiled program. Then is simple as:
File compiledApp = new File('path/to/compiled/app.dart');
compiledApp.lastModified().then(
(modifiedDate)
{
print("This program was compiled $modifiedDate");
},
onError: (exp)
{
// File doesn't exist ?
}
);
This trick build on knowledge that compiler will modify the 'last modify date' of the file

How to force generating a warning in DartEditor

I'm looking for a way to generate a warning anywhere in my code (top level, class, functions). As I'm typically having a 0 warning policy, this enables me to see where I make a change I need to revert before commit
For example in Java, i could do this:
private int warning_revert_to_false;
boolean DEBUG = true;
and it will generate a warning (according to my settings).
Using jslint/jshint that's easy (mixed tab/space for example), in C/C++ i can use pragma...
Basically I want the code to still compile and run and so far in Dart, I could not find a simple solution and I'm sure there is one that I have missed. Thanks!
Add the following library to your program:
library forced;
import 'package:meta/meta.dart';
class Forced {
#deprecated
static void warning() {
}
}
Wherever you want a forced warning you can simply write:
Forced.warning();

Resources