Monkey Patching in Go show different result when running from CLI - unit-testing

I am using Monkey Patching in Go. When I debug the following code in VSCode it shows that the function proc.Signal return the error programmed.
func TestCheckProcessRunning(t *testing.T) {
monkey.Patch((*os.Process).Signal, func(p *os.Process, sig os.Signal) error {
return errors.New("Signal failed")
})
proc := &os.Process{}
sig_e := proc.Signal(syscall.Signal(0))
fmt.Printf("%s\n", sig_e)
}
Signal failed
But when I tried to run the test using go test . , the patch is no longer applied and got a different error:
os: process not initialized
Any idea of what I am doing wrong?

It seems it needed the flag -gcflags=-l
go test -gcflags=-l .

Related

How to refactor rust assert_json_snapshot_matches into assert_json_snapshot

I upgraded my rust insta package to the latest 1.8.0, on the process found assert_json_snapshot_matches has been replaced with assert_json_snapshot. I tried to run the test just by replacing the method. But it's doesn't work.
let actual = &create_oci_json(&site, "whoami").expect("json should be okay");
assert_json_snapshot_matches!("oci_json", &actual, {
".process.user.uid" => 42,
".process.user.gid" => 42,
});
After changing the method name to assert_json_snapshot it throws this error.
let actual = &create_oci_json(&site, "whoami").expect("json should be okay");
let expect = json!({
".process.user.uid" : "42",
".process.user.gid" : "42"
});
assert_json_snapshot!(vec![actual, &expect]);
Edit:
Missed errors:
501 │+]
────────────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
To update snapshots run `cargo insta review`
thread 'container::contained_v2::tests::test_create_oci_json' panicked at 'snapshot assertion for 'oci_json' failed in line 413', /Users/mohamednizar/.cargo/registry/src/github.com-1ecc6299db9ec823/insta-1.8.0/src/runtime.rs:1048:9
Since I was new to the cargo, I forget to run the cargo insta review. After the run of this command, the issue was resolved.

go test unexpectedly passes -test.timeout flag to program under test

I have a small cli program in go that implements one command line flag (-config=<config file>) using flag.String().
When I call go test it errors out because my program says flag provided but not defined: -test.timeout.
go test -v -x -args -config=./config.yml gives me:
/usr/lib/go/pkg/tool/linux_amd64/link -o $WORK/b001/... .../b001/myprogram.test -test.timeout=10m0s -test.v=true -config=./config.yml
In other words, it passes the test arguments on to myprogram, even though I expect this to be inhibited by the -args flag (should only pass arguments after the -args flag?).
Is this a bug, resp. is there a way to inhibit go test from passing any flags to the tested program other than the ones I specifically tell it to)?
EDIT:
The relevant code snippet:
package main
import (
"context"
"encoding/json"
"flag"
// ...
)
// ...
var (
configpath = flag.String("config", "config.yml", "Config file to use, if omitted defaults to config.yml")
// ...
func init() {
var err error
flag.Parse()
_, err = configGlobal.getConfig(*configpath)
if err != nil {
panic("No config loaded")
}
// ...
}
Normally running the program without arguments or with the argument -config ./config.yml works, obviously passing other flags doesn't.

Unknown action hi in contract error while deploying smart contract on testnet

I am tring to deploy contract on testnet http://api.kylin.alohaeos.com. I have following hello2.cpp program
#include <eosiolib/eosio.hpp>
#include <eosiolib/print.hpp>
using namespace eosio;
class suniltestacc : public contract {
public:
using contract::contract;
[[eosio::action]]
void hi( name user ) {
//require_auth( user );
//print( "Hello, ", name{user} );
print( "Hello, ", user);
}
};
EOSIO_DISPATCH(suniltestacc, (hi))
I compiled this program using eosio-cpp -o hello2.wasm hello2.cpp --abigen. It gives me two warnings which I ignored.
Warning, empty ricardian clause file
Warning, empty ricardian clause file
Then I execute cleos -u http://api.kylin.alohaeos.com set contract suniltestacc /home/varsha/Varsha/contracts/hello2
This runs successfully, after that I am trying this command
cleos -u https://api.kylin.alohaeos.com push action suniltestacc hi '["bobmarley"]' -p suniltestacc#active
It is giving me error
error 2019-01-22T08:10:35.520 thread-0 main.cpp:3449 main ] Failed with error: Assert Exception (10)
!action_type.empty(): Unknown action hi in contract suniltestacc.
Can somebody help me with this.
There is nothing wrong with this, Its all working fine.
Below is the result when i called your function.
cleos -u https://api.kylin.alohaeos.com push action suniltestacc hi '["bobmarley"]' -p doslnjslongd#active
executed transaction: 8faa9669ed143c979afa842489dafdfc610e0c6ad4f2831039a170448f867835 104 bytes 134 us
suniltestacc <= suniltestacc::hi {"user":"bobmarley"}
warning: transaction executed locally, but may not be confirmed by the network yet ]
Let me know if i can help you further.

Boost data-driven test output: "Assertion occurred in a following context"

I have a question regarding the output of the following minimal example that uses Boost test.
#define BOOST_TEST_MODULE ExampleTestSuite
#include <boost/test/included/unit_test.hpp>
#include <boost/test/unit_test.hpp>
using boost::unit_test::test_suite;
using boost::unit_test::framework::master_test_suite;
#include <boost/test/data/test_case.hpp>
namespace bdata = boost::unit_test::data;
BOOST_DATA_TEST_CASE(ExampleTest, bdata::xrange(2), testDatum) {
int exp = testDatum == 0 ? 0 : 1;
BOOST_CHECK_EQUAL(testDatum, exp);
}
If I run this test with ./boost_testing --log_level=all, I get the following output:
Running 2 test cases...
Entering test module "ExampleTestSuite"
boost_testing.cpp(11): Entering test suite "ExampleTest"
boost_testing.cpp(11): Entering test case "_0"
boost_testing.cpp(15): info: check testDatum == exp has passed
Assertion occurred in a following context:
testDatum = 0;
boost_testing.cpp(11): Leaving test case "_0"
boost_testing.cpp(11): Entering test case "_1"
boost_testing.cpp(15): info: check testDatum == exp has passed
Assertion occurred in a following context:
testDatum = 1;
boost_testing.cpp(11): Leaving test case "_1"
boost_testing.cpp(11): Leaving test suite "ExampleTest"
Leaving test module "ExampleTestSuite"
*** No errors detected
What is the meaning of the output line Assertion occurred in a following context: testDatum = 0;? Does it indicate an issue with the way that I have set up the data-driven test case or can I safely ignore it?
I agree the message is confusing: this is in fact the description of the assertion context for the test BOOST_CHECK_EQUAL(testDatum, exp), and in fact this is not describing an error, but the context attached to the check. If you change --log_level=all to something else, or user another output format, this should go away.
Feel free to raise an issue on Boost.Test project

Mage_Selenium_Driver::doCommand() should be compatible with that of PHPUnit_Extensions_SeleniumTestCase_Driver

I am trying to configure Magento test automation framework on my system.
When I run phpunit in command line, I am getting following error. Same error I am getting while running test in the netbeans.
Strict Standards: Declaration of Mage_Selenium_Driver::doCommand() should
be compatible with that of PHPUnit_Extensions_SeleniumTestCase_Driver::doCommand()
in C:\MTAF\taf\lib\Mage\Selenium\Driver.php on line 38
..
Fatal error: Call to undefined method PHPUnit_Framework_TestSuite::isPublicTestMethod() in C:\MTAF\taf\lib\Mage\Selenium\TestCase.php on line 2502
Can some one please suggest some solution for the same.
I changed the code, this is the change I performed:
--- a/framework/Mage/Selenium/TestCase.php
+++ b/framework/Mage/Selenium/TestCase.php
## -409,7 +409,7 ## class Mage_Selenium_TestCase extends PHPUnit_Extensions_SeleniumTestCase
$testMethods = array();
$class = new ReflectionClass(self::$_testClass);
foreach ($class->getMethods() as $method) {
- if (PHPUnit_Framework_TestSuite::isPublicTestMethod($method)) {
+ if ($method->isPublic()) {
$testMethods[] = $method->getName();
}
}
I was getting this error when running phpunit >= 4.0. Downgrading to 3.7.x solved it for me.