Can't open a file inside a google test - c++

I have a file in the same directory as the test.
Since I'm using Bazel as the build system (in Linux) I don't have to create the main and initialize the tests (In fact I don't know where the main function resides when using Bazel).
#include <string>
#include <fstream>
//Other include files
#include "gtest/gtest.h"
TEST(read_file_test, read_file) {
std::string input_file("file_path");
graph gr;
graph_loader loader;
ASSERT_TRUE(loader.load(gr, input_file));
}
The BUILD file for the tests:
cc_test(
name = "loaderLib_test",
srcs = ["loaderLib_test.cpp"],
deps = [
"//src/lib/loader:loaderLib",
"#com_google_googletest//:gtest_main",
],
data = glob(["resources/tests/input_graphs/graph_topology/**"]),
)
The WORKSPACE file:
load("#bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
git_repository(
name = "com_google_googletest",
remote = "https://github.com/google/googletest",
tag = "release-1.8.1",
)
The directory structure:
.
├── README.md
├── WORKSPACE
├── bazel-bin -> /home/mmaghous/.cache/bazel/_bazel_mmaghous/35542ec7cbabc2e6f7475e3870a798d1/execroot/__main__/bazel-out/k8-fastbuild/bin
├── bazel-graph_processor -> /home/mmaghous/.cache/bazel/_bazel_mmaghous/35542ec7cbabc2e6f7475e3870a798d1/execroot/__main__
├── bazel-out -> /home/mmaghous/.cache/bazel/_bazel_mmaghous/35542ec7cbabc2e6f7475e3870a798d1/execroot/__main__/bazel-out
├── bazel-testlogs -> /home/mmaghous/.cache/bazel/_bazel_mmaghous/35542ec7cbabc2e6f7475e3870a798d1/execroot/__main__/bazel-out/k8-fastbuild/testlogs
├── resources
│ └── tests
│ └── input_graphs
│ ├── graph_data
│ │ └── G1_data.txt
│ └── graph_topology
│ ├── G1_notFully_notWeakly.txt
│ ├── G2_notFully_Weakly.txt
│ ├── G3_fully_weakly.txt
│ ├── G4_fully_weakly.txt
│ ├── G5_notFully_weakly.txt
│ ├── G6_notFully_weakly.txt
│ └── G7_notFully_notWeakly.txt
├── src
│ ├── lib
│ │ ├── algorithms
│ │ │ ├── BUILD
│ │ │ └── graph_algorithms.hpp
│ │ ├── graph
│ │ │ ├── BUILD
│ │ │ └── graph.hpp
│ │ └── loader
│ │ ├── BUILD
│ │ └── graph_loader.hpp
│ └── main
│ ├── BUILD
│ └── main.cpp
├── tests
│ ├── BUILD
│ ├── algorithmsLib_test.cpp
│ ├── graphLib_test.cpp
│ └── loaderLib_test.cpp
└── todo.md
So how should I reference the file if it's in the same folder as the test or any other folder?
BTW: Using the full path from the root of my file systems works fine.

The problem is that the glob is relative to the BUILD file like when you refer a file in srcs. The glob is expanding to an empty list because no file is matching. The easiest way to see it is putting the full path explicitly instead of the glob and Bazel will complain.
You have two options, or you move the resources folder inside the tests folder or you create a BUILD file in the resources folders where you create a filegroup exposing the txt files and then in the test target you reference the filegroup target.
https://docs.bazel.build/versions/master/be/general.html#filegroup
In resources/BUILD
filegroup(
name = "resources",
srcs = glob(["tests/input_graphs/graph_topology/**"]),
visibility = ["//visibility:public"],
)
In tests/BUILD
cc_test(
name = "loaderLib_test",
srcs = ["loaderLib_test.cpp"],
deps = [
"//src/lib/loader:loaderLib",
"#com_google_googletest//:gtest_main",
],
data = ["//resources"],
)

Related

Terraform aws_s3_bucket_object, Call to function "filemd5" failed: open <file>

My index.html is in the same level of where my terraform resource file is located
terraform
│   └── site
│   ├── index.html
│   ├── s3.tf
│   └── variables.tf
My config looks like this
resource "aws_s3_bucket_object" "index" {
key = "index.html"
acl = "public-read"
bucket = aws_s3_bucket.my_example_site.id
source = "index.html"
etag = filemd5("index.html")
}
When I apply, I get this error
│ Error: Error in function call
│
│ on modules/terraform/site/s3.tf line 15, in resource "aws_s3_bucket_object" "index":
│ 15: etag = filemd5("index.html")
│
│ Call to function "filemd5" failed: open index.html: no such file or directory.
You need a proper reference to your path. You're running terraform out of your root, not the module. Thus as the comments suggest, you need to you ${path.module} to properly tell terraform where the file is.

Add E2E Unit Test to CI/CD Amplify Console

Currently, the amplify console supports E2E tests with Cypress and can show the report and artifacts in a friendly format within the test section of each build. Unfortunately, there is no way in their doc to show unit test results. so, what is the correct way to write amplify.yml to use Unit Test for the backend phase?
I'm using python and
My tree path of mine if needed is:
C:.
├───api
│ └───apiExample
│ └───build
├───auth
│ └───authExample
│ └───build
├───awscloudformation
│ └───build
│ ├───api
│ │ └───apiExample
│ │ └───build
│ │
│ │
│ │
│ │
│ ├───auth
│ │ └───**
│ │ └───build
│ │
│ │
│ │
│ │
│ ├───awscloudformation
│ │ └───build
│ └───function
│ └───exampleHandler
│
├───function
│ └───exampleHandler
│ ├───src
│ │ ├───src.egg-info
│ │ └───__pycache__
│ └───test
│ ├───.pytest_cache
│ │ └───v
│ │ └───cache
│ └───__pycache__
├───hosting
│ └───amplifyhosting
└───types
and my current config is:
version: 1
backend:
phases:
preBuild:
commands:
- export BASE_PATH=$(pwd)
- yum install -y gcc openssl-devel bzip2-devel libffi-devel python3.8-pip
- cd /opt && wget https://www.python.org/ftp/python/3.8.2/Python-3.8.2.tgz
- cd /opt && tar xzf Python-3.8.2.tgz
- cd /opt/Python-3.8.2 && ./configure --enable-optimizations
- cd /opt/Python-3.8.2 && make altinstall
- pip3.8 install --user pipenv
- ln -fs /usr/local/bin/python3.8 /usr/bin/python3
- ln -fs /usr/local/bin/pip3.8 /usr/bin/pip3
- cd $BASE_PATH
build:
commands:
- '# Execute Amplify CLI with the helper script'
- amplifyPush --simple
frontend:
phases:
preBuild:
commands:
- npm ci
build:
commands:
- npm run build
artifacts:
baseDirectory: build
files:
- '**/*'
cache:
paths:
- node_modules/**/*

Using CMake and not able to run program without getting "LINK : fatal error LNK1104: cannot open file 'src.lib'" error

I'm trying to get my head around CMake and have been testing it out in Visual Studio Code on Windows 10 with a simple project that has a couple of header files and compiles with no issues when done manually. I've run cmake .. successfully from the build folder, but upon running cmake --build . I get the above error in reference to my weight_converter.vcxproj file.
I've done a load of searching online but can't find anything that answers what is going on.
From other results I've seen some suggestions in Visual Studio to add <file>.lib to Project Options -> Linker -> Input -> Additional Dependencies on visual studio, but I'm on visual studio code and can't find a corresponding setting. I've found the .vcxproj file in my project, and although I don't really know what's going on in it, src.lib is written next to the <Link>/<AdditionalDependencies> headings.
This is my main CMakeLists.txt file contents for reference:
# set minimum CMake version, project name, and C++ standard
cmake_minimum_required(VERSION 3.19.4)
project("weight_converter")
add_subdirectory(src)
add_executable(weight_converter weight_converter.cpp )
target_include_directories("${PROJECT_NAME}" PUBLIC "${PROJECT_BINARY_DIR}" "${PROJECT_SOURCE_DIR}/src")
file(MAKE_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin )
file(MAKE_DIRECTORY ${CMAKE_SOURCE_DIR}/lib)
SET(LIBRARY_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/lib)
target_link_libraries("${PROJECT_NAME}" PUBLIC src)
And this is the CMakeLists.txt file in my src folder:
add_library(imperial_to_metric imperial_to_metric.cpp)
add_library(metric_to_imperial metric_to_imperial.cpp)
This is my project structure if it matters:
├───.vscode
├───bin
│ └───Debug
├───build
│ ├───.cmake
│ │ └───api
│ │ └───v1
│ │ ├───query
│ │ │ └───client-vscode
│ │ └───reply
│ ├───CMakeFiles
│ │ ├───3.19.4
│ │ │ ├───CompilerIdC
│ │ │ │ ├───Debug
│ │ │ │ │ └───CompilerIdC.tlog
│ │ │ │ └───tmp
│ │ │ ├───CompilerIdCXX
│ │ │ │ ├───Debug
│ │ │ │ │ └───CompilerIdCXX.tlog
│ │ │ │ └───tmp
│ │ │ └───x64
│ │ │ └───Debug
│ │ │ └───VCTargetsPath.tlog
│ │ ├───CMakeTmp
│ │ └───fa0880fffde885133f10c0b2cfeb0cbc
│ ├───Debug
│ ├───src
│ │ ├───CMakeFiles
│ │ ├───Debug
│ │ ├───imperial_to_metric.dir
│ │ │ └───Debug
│ │ │ └───imperial.31E5CD06.tlog
│ │ └───metric_to_imperial.dir
│ │ └───Debug
│ │ └───metric_t.0BAA5631.tlog
│ ├───weight_converter.dir
│ │ └───Debug
│ │ └───weight_converter.tlog
│ └───x64
│ └───Debug
│ └───ZERO_CHECK
│ └───ZERO_CHECK.tlog
├───lib
├───src
│ └───CMakeLists.txt
│ └───imperial_to_metric.cpp
│ └───metric_to_imperial.cpp
│ └───imperial_to_metric.h
│ └───metric_to_imperial.h
│
├───CMakeLists.txt
└───weight_converter.cpp
The underlying problem is this:
target_link_libraries("${PROJECT_NAME}" PUBLIC src)
This tells CMake to link to a library named src. However in the src/CMakeLists.txt the libraries are called. imperial_to_metric and metric_to_imperial
add_library(imperial_to_metric imperial_to_metric.cpp)
add_library(metric_to_imperial metric_to_imperial.cpp)
So what one probably wants is
target_link_libraries("${PROJECT_NAME}" PUBLIC imperial_to_metric metric_to_imperial)
A minor nit, but it is often better practice to have the library targets specify the build requirements. So instead of:
target_include_directories("${PROJECT_NAME}" PUBLIC "${PROJECT_BINARY_DIR}" "${PROJECT_SOURCE_DIR}/src")
one would have the following in src/CMakeLists.txt
target_include_directories(metric_to_imperial PUBLIC ".")
target_include_directories(imperial_to_metric PUBLIC ".")
This way any consumer of imperial_to_metric gets the needed include directories simply by the target_link_libraries command.

Using outputs from other tf files in terraform

Is there a way of using output values of a module that is located in another folder? Imagine the following environment:
tm-project/
├── lambda
│   └── vpctm-manager.js
├── networking
│   ├── init.tf
│   ├── terraform.tfvars
│   ├── variables.tf
│   └── vpc-tst.tf
├── prd
│   ├── init.tf
│   ├── instances.tf
│   ├── terraform.tfvars
│   └── variables.tf
└── security
└── init.tf
I want to create EC2 instances and place them in a subnet that is declared in networking folder. So, I was wondering if by any chance I could access the outputs of the module I used in networking/vpc-tst.tf as the inputs of my prd/instances.tf.
Thanks in advances.
You can use a outputs.tf file to define the outputs of a terraform module. Your output will have the variables name such as the content below.
output "vpc_id" {
value = "${aws_vpc.default.id}"
}
These can then be referenced within your prd/instances.tf by referencing the resource name combined with the output name you defined in your file.
For example if you have a module named vpc which uses this module you could then use the output similar to below.
module "vpc" {
......
}
resource "aws_security_group" "my_sg" {
vpc_id = module.vpc.vpc_id
}

Terraform: Passing variable from one module to another

I am creating a Terraform module for AWS VPC creation.
Here is my directory structure
➢ tree -L 3
.
├── main.tf
├── modules
│   ├── subnets
│   │   ├── main.tf
│   │   ├── outputs.tf
│   │   └── variables.tf
│   └── vpc
│   ├── main.tf
│   ├── outputs.tf
│   └── variables.tf
└── variables.tf
3 directories, 12 files
In the subnets module, I want to grab the vpc id of the vpc (sub)module.
In modules/vpc/outputs.tf I use:
output "my_vpc_id" {
value = "${aws_vpc.my_vpc.id}"
}
Will this be enough for me doing the following in modules/subnets/main.tf ?
resource "aws_subnet" "env_vpc_sn" {
...
vpc_id = "${aws_vpc.my_vpc.id}"
}
Your main.tf (or wherever you use the subnet module) would need to pass this in from the output of the VPC module and your subnet module needs to take is a required variable.
To access a module's output you need to reference it as module.<MODULE NAME>.<OUTPUT NAME>:
In a parent module, outputs of child modules are available in expressions as module... For example, if a child module named web_server declared an output named instance_ip_addr, you could access that value as module.web_server.instance_ip_addr.
So your main.tf would look something like this:
module "vpc" {
# ...
}
module "subnets" {
vpc_id = "${module.vpc.my_vpc_id}"
# ...
}
and subnets/variables.tf would look like this:
variable "vpc_id" {}