How to organize test resource files when using waf - c++

I am using Google Tests in a project with waf as a build system. I want to know an effective way of dealing with resource files.
For a directory structure like the following:
MyProject
├── build
├── src
| ├──do_something.cpp
| ├──do_something.h
├── test
| ├── unit_test.cpp
│ ├── resources
│ │ ├── input1.txt
│ │ ├── input2.txt
├── wscript
After building, to run the tests from the build directory, I would need the resource files to be copied across. The build directory would look like:
MyProject
├── build
| ├── test
│ │ ├── resources
│ │ | ├── input1.txt
│ │ | ├── input2.txt
│ │ ├── unit_test
To achieve this, my current wscript is:
def options(opt):
opt.load('compiler_cxx')
def configure(conf):
conf.load('compiler_cxx')
def build(bld):
bld.stlib(source='src/do_something.cpp',
target='mylib',
includes='src')
bld.exec_command("cp -r test/resources build/test")
bld.program(source='test/unit_test.cpp',
includes='src',
target='test/unit_test',
use='mylib')
Using the bld.exec_command like this seems hacky. What is a better way? How are other people organizing their test resources with waf?
I am using waf 1.9.5.

The easiest way is of course to change your program so that it can read resources from an arbitrary location. Why litter the file system with multiple copies of the same files?
That said, you can easily copy a directory tree recursively using:
for f in ctx.path.ant_glob('tests/resources/**/*'):
ctx(features = 'subst', source = f.srcpath(),
target = f.srcpath())

Related

How to put projects in one folder in a Visual Studio solution

I did some problem-solving in C++, and the current file structure looks like this.
solution_folder/
├── question_1/
│ ├── Main.cpp
│ ├── question_1.vcxproj
│ └── question_1.vcxproj.filters
├── question_2/...
├── (more project folders)
└── solution.sln
I want to put all projects in one folder and still be able to open the solution in Visual Studio just as I used to before. I don't want to scroll through hundreds of folders to get to the readme section when I upload the solution on GitHub.
solution_folder/
├── src/
│ ├── question_1/
│ │ ├── Main.cpp
│ │ ├── question_1.vcxproj
│ │ └── question_1.vcxproj.filters
│ ├── question_2/...
│ └── (more project folders)
└── solution.sln
Is this possible? Should I not create a project for each question?
As #drescherjm and #heapunderrun commented,
Put all project folders in one folder in File Explorer,
Remove all projects from the solution in Solution Explorer,
Right-click on the solution and Add → Existing Project all projects.
Update:
Shortcuts
Remove: Delete
Add existing project: Alt F D E

How do I run unit tests from smaller applications inside of a parent directory?

I'm working in a repository with multiple smaller applications for various lambdas. I'd like to be able to run cargo test from the top level directory, but I can't seem to find a way to get this to work since the files aren't nested within a top level src directory.
├── cloudformation
├── apps
│ ├── app1
│ │ └── src
│ └── app2
│ └── src
└── otherStuff
Ideally I could run cargo test from the top level and it would dig into apps and run tests from the src directory nested within each individual app. Is there a way to accomplish this?

Is Nlog not archiving all files?

I'm logging one file per day, and I want to move the old ones to a subfolder every month. I'm feeling there is something off here.
These are my current settings. For testing purposes, I changed it to archive every minute:
<target name="file" xsi:type="File"
layout="${longdate} ${logger} ${message}${exception:format=ToString}"
fileName="${basedir}/LOG/${date:format=yyyy-MM-dd}.log"
archiveFileName="${basedir}/LOG/OLD_LOGS/{#}.log"
archiveDateFormat="yyyy-MM-dd"
archiveNumbering="Date"
archiveEvery="Minute"
/>
Consider the actual date for this test: May 29th 2020.
If I run my app now, the file 2020-05-29.log will be created in the LOG folder.
.
├── ...
├── LOG
│ └── 2020-05-29.log
└── ...
If I run my app one minute later, the file above will be archived to the newly created subfolder OLD_LOGS and a new file will be created in the LOG folder.
.
├── ...
├── LOG
│ ├── 2020-05-29.log
│ ├── OLD_LOGS
│ └── 2020-05-29.log
└── ...
If I change my clock to the next day (May 30th 2020) a new file will be created 2020-05-30.log
.
├── ...
├── LOG
│ ├── 2020-05-29.log
│ ├── 2020-05-30.log
│ ├── OLD_LOGS
│ └── 2020-05-29.log
└── ...
If I run my app one minute later, the 2020-05-30.log file will be archived
.
├── ...
├── LOG
│ ├── 2020-05-29.log
│ ├── 2020-05-30.log
│ ├── OLD_LOGS
│ ├── 2020-05-29.log
│ └── 2020-05-30.log
└── ...
Shouldn't the LOG/2020-05-29.log file have been archived?
There is a problem in your configuration for per minute archival.
NLog starts overriding the old file because your file format is yyyy-MM-DD.
The correct configuration for per minute achieving, add below code.
<target name="kFile" xsi:type="File"
layout="${longdate} ${logger} ${message}${exception:format=ToString}"
fileName="${basedir}/LOG/${date:format=yyyy-MM-dd}.log"
archiveFileName="${basedir}/LOG/OLD_LOGS/{#}.log"
archiveDateFormat="yyyy-MM-dd-mm"
archiveNumbering="Date"
archiveEvery="Minute"
/>
Changed format to yyyy-MM-dd-mm.
This change created below files in my folder
.
├── ...
├── LOG
│ ├── 2020-05-30.log
│ ├── OLD_LOGS
│ ├── 2020-05-30-01.log
│ └── 2020-05-30-02.log
└── ...
I checked with the same configuration as yours
and the result was
So the NLog archiving feature does not work the way you want it to. It just places the last file of the month to the archive folder not all.
So, you have to configure NLog to archive files every day and set max file number to something like 365 to keep them for one year.
Apply below configuration:
<target name="kFile" xsi:type="File"
layout="${longdate} ${logger} ${message}${exception:format=ToString}"
fileName="${basedir}/LOG/${date:format=yyyy-MM-dd}.log"
archiveFileName="${basedir}/LOG/OLD_LOGS/{#}.log"
archiveDateFormat="yyyy-MM-dd"
archiveNumbering="Date"
archiveEvery="Day"
maxArchiveFiles="365"
/>
This way you will get one file per day and the file will be transferred to the archive folder each day. The archive will keep the file for one year. You may want to review the max archive file though. Because it may increase the size of the folder based on how much you log.

Django & coverage - .coveragerc omit not working

I have a Django project with multiple applications. In one application, I have a library already tested using unittest + coverage.
When I run the test of the django project, I would like to omit this folder.
My project architecture is :
project/
├── application1/
│ ├── tests.py
│ ├── views.py
│ ├── ...
│ └── my_lib/ << The lib I want to omit
│ ├── tests.py
│ ├── script.py
│ ├── __init__.py
│ └── ...
├── application2/
│ ├── tests.py
│ ├── views.py
│ └── ...
├── application3/
│ ├── tests.py
│ ├── views.py
│ └── ...
├── .coveragerc
├── runtest.bat
├── manage.py
└── ...
runtest.bat is :
CALL activate ./venv
coverage erase
coverage run manage.py test
coverage html
PAUSE
Based on several tutorials and SO questions, I've tried several .coveragerc files but none of them properly skipped the library. Testing it creates a fail because it tries to load with the incorrect relative path.
.coveragerc is :
[run]
omit =
*/application1/my_lib/*
[report]
exclude_lines =
if __name__ == .__main__.:
show_missing = True
I also tried :
[run]
source = .
omit =
/application1/my_lib/*
[run]
source = project/*
omit =
*/application1/my_lib/*
[run]
source = .
omit =
*/application1/my_lib/*
Do you have any clue what am I doing wrong ?
For information :
Django version is 2.2.5
Coverage version is 4.5.4
Python version is 3.7
Few sources :
Django Coverage
Coverage Documentation
SO question
Thanks in advance
EDIT 1:
Just as a background. my_lib is mainly a file containing a class. This code is just embedded in the application to be used "like" a standard library.
In application1/views.py, I simply have a from . import my_lib.
In application1/my_lib/__init__.py, I have from .script import MyClass
The objective is simply to be able then to use in my view with my_lib.MyClass.do_something()
Now the reason why I would like to exclude this "library" from the coverage is because this was developped out of the application. It has his own unittest in applications1/my_lib/tests/py starting with from script import MyClass.
When I run the coverage in the root of the project, Python cannot find script.py in the root of the project so it triggers the error
File "path_to/project/application1/my_lib/tests.py", line 3
from script import MyClass
ModuleNotFoundError: No module named script.
In the worst case scenario, I could put this library to site-packages of my virtual environment bu I would like to avoid it (there will be probably multiple similar my_lib with at most 1 per application)
EDIT 2:
As a temporary solution, I simply renamed application1/my_lib/tests.py by application1/my_lib/script_tests.py. There is no file starting by test so the coverage does not care about this folder anymore.
The other alternative to run the test at the same time as the project required quite a lot of updates due to relative path used to several files

Terraform environments - how to get it DRY

We are utilizing Terraform heavily for AWS Cloud provisioning. Our base terraform structure looks like this:
├─ modules
├── x
├── y
├─ environments
├── dev
│ ├── main.tf
│ ├── output.tf
│ └── variables.tf
└── uat
│ ├── main.tf
│ ├── output.tf
│ └── variables.tf
└── prod
├── main.tf
├── output.tf
└── variables.tf
As we reached a point where we have many modules and many environments, code duplication becomes a more serious headache now, we would like to get rid of as much of it as possible.
Our main concern currently is with the output.tf files - every time we extend an existing module or add a new module, we need to set up the environment specific configuration for it (this is expected), but we still have to copy/paste the required parts into output.tf to output the results of the provisioning (like IP addresses, AWS ARNs, etc.).
Is there a way to get rid of the duplicated output.tf files? Could we just define the wanted outputs in the modules themselves and see all defined outputs whenever we run terraform for a specific environment?
We built and open sourced Terragrunt to solve this very issue. One of Terragrunt's features is the ability to download remote Terraform configurations. The idea is that you define the Terraform code for your infrastructure just once, in a single repo, called, for example, modules:
└── modules
├── app
│ └── main.tf
├── mysql
│ └── main.tf
└── vpc
└── main.tf
This repo contains typical Terraform code, with one difference: anything in your code that should be different between environments should be exposed as an input variable. For example, the app module might expose the following variables:
variable "instance_count" {
description = "How many servers to run"
}
variable "instance_type" {
description = "What kind of servers to run (e.g. t2.large)"
}
In a separate repo, called, for example, live, you define the code for all of your environments, which now consists of just one .tfvars file per component (e.g. app/terraform.tfvars, mysql/terraform.tfvars, etc). This gives you the following file layout:
└── live
├── prod
│ ├── app
│ │ └── terraform.tfvars
│ ├── mysql
│ │ └── terraform.tfvars
│ └── vpc
│ └── terraform.tfvars
├── qa
│ ├── app
│ │ └── terraform.tfvars
│ ├── mysql
│ │ └── terraform.tfvars
│ └── vpc
│ └── terraform.tfvars
└── stage
├── app
│ └── terraform.tfvars
├── mysql
│ └── terraform.tfvars
└── vpc
└── terraform.tfvars
Notice how there are no Terraform configurations (.tf files) in any of the folders. Instead, each .tfvars file specifies a terraform { ... } block that specifies from where to download the Terraform code, as well as the environment-specific values for the input variables in that Terraform code. For example, stage/app/terraform.tfvars may look like this:
terragrunt = {
terraform {
source = "git::git#github.com:foo/modules.git//app?ref=v0.0.3"
}
}
instance_count = 3
instance_type = "t2.micro"
And prod/app/terraform.tfvars may look like this:
terragrunt = {
terraform {
source = "git::git#github.com:foo/modules.git//app?ref=v0.0.1"
}
}
instance_count = 10
instance_type = "m2.large"
See the Terragrunt documentation for more info.
One way to resolve this is to create a base environment, and then symlink the common elements, for example:
├─ modules
├── x
├── y
├─ environments
├── base
│ ├── output.tf
│ └── variables.tf
├── dev
│ ├── main.tf
│ ├── output.tf -> ../base/output.tf
│ └── variables.tf -> ../base/variables.tf
├── uat
│ ├── main.tf
│ ├── output.tf -> ../base/output.tf
│ └── variables.tf -> ../base/variables.tf
├── super_custom
│ ├── main.tf
│ ├── output.tf # not symlinked
│ └── variables.tf # not symlinked
└── prod
├── main.tf
├── output.tf -> ../base/output.tf
└── variables.tf -> ../base/variables.tf
This approach only really works if your output.tf and variables.tf files are the same for each environment, and although you can have non-symlinked variants (e.g. super_custom above), this can become confusing as it's not immediately obvious which environments are custom and which aren't. YMMV. I try to keep the changes between environments limited to a .tfvars file per environment.
It's worth reading Charity Major's excellent post on tfstate files, which set me on this path.
If your dev, uat and prod environments have the same shape, but different properties you could leverage workspaces to separate your environment state, along with separate *.tfvars files to specify the different configurations.
This could look like:
├─ modules
│ ├── x
│ └── y
├── dev.tfvars
├── prod.tfvars
├── uat.tfvars
├── main.tf
├── outputs.tf
└── variables.tf
You can create a new workspace with:
terraform workspace new uat
Then deploying changes becomes:
terraform workspace select uat
terraform apply --var-file=uat.tfvars
The workspaces feature ensures that different environments states are managed separately, which is a bonus.
This approach only works when the differences between the environments are small enough that it makes sense to encapsulate the logic for that in the individual modules (for example, having a high_availability flag which adds some additional redundant infrastructure for uat and prod).