How to use check library with CTest for unit testing in C - c

I have project in which I want to use check library for unit testing.
My current project is using CMake and has following structure:
.
├── CMakeLists.txt
├── COPYING
├── ChangeLog
├── README
├── src
│   ├── core
│ │   ├── CMakeLists.txt
│ │ └── main.c
│   ├── core-test
│ │   ├── CMakeLists.txt
│ │ └── main.c
│ └── scrypt
└── doc
└── protocol.txt
In core-test I have unit tests for core module. In scrypt-test I will have tests for scrypt module and so on.
Does using check with CTest make sense?
If yes: how do I connect unit test that use check to CMake/CTest project? Do I need additional configuration for CTest to interpret results from check-enabled executables?

Using check with CTest is possible, unfortunately not convenient.
When unit tests using check fail they will be counted as a failure in CTest. The problem is, that CTest doesn't show output of application that failed1. The other downside is that CTest doesn't count individual check tests, so a "single" failed test could mean multiple check tests. I wasn't able to find a way to make CTest interpret check results.
You can force printing of output by creating CTestCustom.cmake file in build directory (the same one in which you run make or ctest) with following contents:
set (CTEST_CUSTOM_POST_TEST "cat Testing/Temporary/LastTest.log")

Related

CMake import external library (GNU readline) using ExternalProject_Add()

I am really new (just a few weeks) to C and its bulding tools. Using the CMake documetation and some other guides I created a basic CMake project. This works as expectd.
Now I wanted to use some libraries in my project (GNU readline and cJSON).
The cJSON library also uses CMake so I managed to include it in my project without too much trouble.
The GNU readline library on the other hand uses ninja and autotools (if I'm not mistaken). This is where I'm facing issues.
I am trying to build the GNU readline library to link the static files later in my project.
In the CMake documetation I saw you could use ExternalProject_Add() for the purpose of configuring and building an external project.
Just to test this I created a simple Project using CMake with ExternalProject_Add().
My test project has the following layout:
test
├── cmake-build-debug
│   ├── CMakeFiles
│   │   ├── 3.23.2
│   │   │   └── CompilerIdC
│   │   │   └── tmp
│   │   ├── CMakeTmp
│   │   ├── readline.dir
│   │   └── test.dir
│   ├── readline-prefix
│   │   ├── src
│   │   │   ├── readline-build
│   │   │   └── readline-stamp
│   │   └── tmp
│   └── Testing
│   └── Temporary
├── install_dir
└── libraries
└── readline-8.2
├── doc
├── examples
│   ├── autoconf
│   └── rlfe
├── m4
├── shlib
└── support
The library source files are located in test/libraries/readline-8.2.
I want the build files to get installed in the test/install_dir.
With the following Code I manage to build the the files, but I get errors when building it.
This is the content of my CMakeLists.txt
cmake_minimum_required(VERSION 3.23)
project(test C)
set(CMAKE_C_STANDARD 11)
set(LIB_PATH ${CMAKE_SOURCE_DIR}/libraries)
set(READLINE_LIB_SRC ${LIB_PATH}/readline-8.2)
set(CONFIGURE_ARGS --enable-shared=no --enable-install-examples=no --prefix=${CMAKE_SOURCE_DIR}/install_dir)
add_executable(test main.c)
include(ExternalProject)
ExternalProject_Add(
readline
BUILD_IN_SOURCE TRUE
BUILD_ALWAYS TRUE
SOURCE_DIR ${READLINE_LIB_SRC}
CONFIGURE_COMMAND ${READLINE_LIB_SRC}/configure ${CONFIGURE_ARGS}
BUILD_COMMAND make
INSTALL_COMMAND make install
)
I also tried adding the BUILD_BYPRODUCTS option of the ExternalProject_Add, as I read that ninja could face problems detecting targets otherwise, but without success.
The errors I get in the install step are:
mv: cannot stat '/home/devel/CLionProjects/test/install_dir/lib/libreadline.a': No such file or directory
mv: cannot stat '/home/devel/CLionProjects/test/install_dir/lib/libhistory.a': No such file or directory
But when I look at these directories the files exist.
The output of the build and the error message can be found here: https://gist.github.com/symb10sis/041f879ef5fbd02bcfd9200746101890
I couldn't include it in this question directly as it would get flagged as spam otherwise.
I spent hours reading the documetation, guides and questions here on stackoverflow but with my current knowledge of CMake I am stuck.
What causes the build to think these files don't exist? Is my use of ExternalProject_Add() appropriate for this or am I missing something?
I would really appreciate any help :)

Jest doesn't follow files in a symlink folder and tries to use the main shared folder

The question is - how to force Jest to follow symlinked shared folder file structure but not main shared folder?
I have the next files structure:
root
├── projects
│ ├── A
│ │ ├── node_modules
│ │ ├── shared (symlink ../../shared-main)
│ │ ├── components
│ │ ├── settings.ts
│ ├── B
│ │ ├── node_modules
│ │ ├── shared (symlink ../../shared-main)
│ │ ├── components
│ │ ├── settings.ts
├── shared-main
│ ├── utils.ts
│ ├── config.ts
In my projects, A and B, I use utils from the shared folder. Utils.ts uses config.ts where imports settings files by path './settings.ts', but inside the shared-main folder, it looks like "file doesn't exist" (it is ok). But Jest in a project's tests when it meets using a shared file test fails with the error: "../../shared-main/config.ts:9:35 - error TS2307: Cannot find module '../settings' or its corresponding type declarations."
How to get around this and force Jest to use config.ts from the symlink folder instead of the main one?
How about "testRegex": ["test/.*.[jt]s"], in your jest config so it doesn't try and find shared files, if you're using shared files across multiple projects then no single project should test those files, they should only test their own files.
However I'm not so sure that symlinks are your problem, when using typescript like this and trying to use files outside of your project rootDir it will not find the types for it or it will complain that it can't find the types for it if you've added it to tsconfig include/exclude. Right now I can only assume that you're using tsc --project with specific config files for each project.
// tsconfig.json
"include": ["projectA/**/*.ts"],
If you've done something like that then it won't find any types outside of projectA so anything in config.ts and utils.ts will not have any types or be able to find any modules, unless they're included in your tsconfig.
To show a simpler example if I have:
// tsconfig.json
...
includes: ["src/**/*.ts"]
...
Along with a directory structure like this:
- tsconfig.json
- example.ts
- src
Then anything in example.ts will not be able to find its types or module imports.
The way I get around this issue in my project is to use ts-jest along with specifying where to find tests, along with overriding the globals rootDir.
// .jestrc.json
"testRegex": ["test/.*.[jt]s"],
...
"globals": {
"ts-jest": {
"tsconfig": {
"rootDir": "."
}
}
}

How the use unit test in cmake in language C?

I have a C project:
.
├── build
├── bin
│   └── parser
├── CMakeLists.txt
├── configure.h.in
├── inc
│   ├── configure.h
│   ├── diam_dict.h
│   └── unit_tests.h
├── README.txt
├── src
│   └── diam_dict.c
└── testing
└── unit_tests.c
I want to put unit tests in the directory "testing", in the file "unit_tests.c".
I want to test all the functions of the file "src/diam_dict.c", what should I do to let "unit_tests.c" know where are the functions to test?
[what should I write in the file "unit_tests.c" and the top level cmake file "CMakeLists.txt"?]
(I should call all the functions of the source code file "diam_dict.c" to test them)
ps: The header file "diam_dict.h" contains all the prototypes of the functions in the source file "diam_dict.c", and the header file "unit_tests.h" cotains all the prototypes of the test file "unit_tests.c".
CMake is a build system, so you can combine it with any unit testing framework you wish.
As a matter of fact, CTest can call executables and check their result value, which allows some testing. But it is usually less fine granular compared to real unit test a known from dedicated unit testing frameworks.

Angular folder structure for CMS

i’ve juz started with angular, i saw this post on folder structuring http://www.johnpapa.net/angular-growth-structure/
This is what my folder looks like which i'm building for an eCommerce site frontend:
.
├── index.html
├── css
├── images
├── fonts
├── scripts
│ ├── app.js
│ ├── directives
│ │ ├── search
│ │ ├── image-slider
│ │ ├── faq
│ │ └── form
│ └── api
└── templates
├── nav.html
├── footer.html
└── page
├── full.html
└── sidebar-right.html
Is there any better way/practice that you would do for this?
My greatest worry would be moving on into integrations with a CMS(opencart/magento) folder structure.
Your dir structure looks fine, however you may run into issues while integrating with any CMS as they put their views on a different directory.
Workaround: Once you do integrate Magento or some random CMS, move your views to the CMS's view folder and make sure you serve index.html with your angular from the CMS.
Hope I could help.

Load custom library while using CDN

I have an app that uses a custom application-specific library (sap.ui.foo) which contains custom controls, views and controllers.
My deployment strategy is serving my custom library from the same server/port that is serving the index.html file.
I would also like to use SAP's CDN to load the OpenUI5 libraries (sap.m, etc).
I am using the Grunt/node tools that come with OpenUI5's GitHub repository.
When I load my application all locally (no CDN) it works perfectly, but is very slow (such a huge download payload I suppose) so I'm trying to use the CDN in hopes of improving startup performance.
My index.html looks like this: (edited after #codeworrior's answer):
<script id="sap-ui-bootstrap"
src="https://openui5.hana.ondemand.com/resources/sap-ui-core.js"
data-sap-ui-theme="sap_bluecrystal"
data-sap-ui-libs="sap.ui.layout, sap.m, sap.ui.foo"
data-sap-ui-xx-bindingSyntax="complex"
data-sap-ui-resourceroots='{
"ns":"./",
"sap.ui.foo": "./sap/ui/foo/"
}'
>
Here is my directory structure (which to my knowledge is "standard"):
src
├── foo
│ └── src
│ └── main
│ └── webapp
│ ├── index.html # start point
│ ├── resources
│ ├── test-resources
│ └── WEB-INF
├── sap.m
├── sap.ui.commons
...other sap libs...
└── sap.ui.foo
└── src
└── sap
└── ui
└── foo
└── # my controls...
and after a grunt build:production my "target" directory looks like this:
target
├── openui5-sap.m
├── openui5-sap.ui.commons
├── openui5-sap.ui.core
├── openui5-sap.ui.demokit
├── ...other sap libs...
├── openui5-sap.ui.foo
└── resources
└── sap
└── ui
└── foo
├── Bootstrap.js
├── controllers
├── controls
├── data
├── font
├── img
├── js
├── library.js
├── library-preload.json
├── models
├── tasks
├── themes
├── util.js
├── views
└── wrappers
But, after I do a grunt serve:target and hit the url http://localhost:8989/foo/, in Chrome's dev-tools I get:
failed to preload 'sap.ui.foo.library-preload': Not Found - sap.ui.ModuleSystem
Uncaught Error: failed to load 'sap/ui/foo/library.js' from ./sap/ui/foo/library.js: 404 - Not Found
The network tab shows me that the CDN files are being served just fine, but the files that I'm trying to serve locally (such as my custom lib's library.js and library-preload.json) are 404's.
Any advice on how to get my library to load?
If your library is stored in the usual way (reflecting the full qualified names in the folder structure), then it should be sufficient to define a corresponding entry in your data-sap-ui-resourceroots attribute:
<script id="sap-ui-bootstrap"
src="https://openui5.hana.ondemand.com/resources/sap-ui-core.js"
data-sap-ui-theme="sap_bluecrystal"
data-sap-ui-libs="sap.ui.layout, sap.m, my.uilib"
data-sap-ui-xx-bindingSyntax="complex"
data-sap-ui-resourceroots="{
ns:'./',
'my.uilib': './my/uilib/'
}">
</script>
If the structure is different, just adapt the path in the configuration. resourceRoots are configured early, so you could even specify your lib in the data-sap-ui-libs attribute.
But maybe you tried that already and it didn't work. Then the problem might have been with the spelling of the option. It's 'resourceroots', not 'resource-roots'.

Resources