Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unit tests #4259

Merged
merged 14 commits into from
Aug 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ matrix:
allow_failures:
# temporary until we find out how to prevent (spurious) build failures
- env: TARGET="docs"
- env: TARGET="linux"
before_install:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arturoc I very much would prefer if we left the linux build in allow_failures, so that we can still get a feeling how often the build fails due to memory concerns, and if whatever measures we implement have a meaningful impact.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, this stil has some builds failing because of memory issues: https://travis-ci.org/openframeworks/openFrameworks/jobs/75839351#L1494

# should source before_install.sh so we can export CXX in the linux script.
# instead, export CXX here, because sourcing messes up Travis log output
Expand Down
1 change: 1 addition & 0 deletions addons/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ ofxAndroid/ofAndroidLib/gen
!ofxVectorGraphics
!ofxXmlSettings
!ofxEmscripten
!ofxUnitTests

# don't ignore the .gitignore file
!.gitignore
122 changes: 122 additions & 0 deletions addons/ofxUnitTests/src/ofxUnitTests.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* ofxUnitTestsApp.h
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this header doesn't match the filename?

*
* Created on: Jul 8, 2015
* Author: arturo
*/
#pragma once

#include "ofConstants.h"
#include "ofLog.h"
#include "ofBaseApp.h"
#include "ofAppRunner.h"
#include <string>

class ofColorsLoggerChannel: public ofBaseLoggerChannel{
std::string CON_DEFAULT="\033[0m";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do these flags work on non-unixes? if not, might be a good idea to formulate this in a cross-platform way.

std::string CON_BOLD="\033[1m";
std::string CON_RED="\033[31m";
std::string CON_YELLOW="\033[33m";
std::string CON_GREEN="\033[32m";
std::string getColor(ofLogLevel level) const{
switch(level){
case OF_LOG_FATAL_ERROR:
case OF_LOG_ERROR:
return CON_RED;
case OF_LOG_WARNING:
return CON_YELLOW;
case OF_LOG_NOTICE:
return CON_GREEN;
default:
return CON_DEFAULT;
}
}
public:
void log(ofLogLevel level, const std::string & module, const std::string & message){
// print to cerr for OF_LOG_ERROR and OF_LOG_FATAL_ERROR, everything else to cout
std::cout << "[" << ofGetLogLevelName(level, true) << "] ";
// only print the module name if it's not ""
if(module != ""){
std::cout << module << ": ";
}
std::cout << CON_BOLD << getColor(level) << message << CON_DEFAULT << std::endl;
}

void log(ofLogLevel level, const std::string & module, const char* format, ...){
va_list args;
va_start(args, format);
log(level, module, format, args);
va_end(args);
}

void log(ofLogLevel level, const std::string & module, const char* format, va_list args){
//thanks stefan!
//http://www.ozzu.com/cpp-tutorials/tutorial-writing-custom-printf-wrapper-function-t89166.html
fprintf(stdout, (CON_BOLD + getColor(level) + "[%s] " + CON_DEFAULT).c_str(), ofGetLogLevelName(level, true).c_str());
if(module != ""){
fprintf(stdout, "%s: ", module.c_str());
}
vfprintf(stdout, format, args);
fprintf(stdout, "\n");
}
};

class ofxUnitTestsApp: public ofBaseApp{
void setup(){
ofSetLoggerChannel(std::shared_ptr<ofBaseLoggerChannel>(new ofColorsLoggerChannel));
run();

if(numTestsFailed == 0){
ofLogNotice() << numTestsPassed << "/" << numTestsTotal << " tests passed";
}else{
ofLogError() << numTestsFailed << "/" << numTestsTotal << " tests failed";
}
ofExit(numTestsFailed);
}
virtual void run() = 0;

protected:
void test(bool test, const std::string & testName, const std::string & msg, const std::string & file, int line){
numTestsTotal++;
if(test){
ofLogNotice() << testName << " passed";
numTestsPassed++;
}else{
ofLogError() << testName << " failed " << msg;
ofLogError() << file << ": " << line;
numTestsFailed++;
}
}

void test(bool test, const std::string & testName, const std::string & file, int line){
this->test(test,testName,"",file,line);
}

template<typename T1, typename T2>
void test_eq(T1 t1, T2 t2, const std::string & testName, const std::string & msg, const std::string & file, int line){
numTestsTotal++;
if(t1==t2){
ofLogNotice() << testName << " passed";
numTestsPassed++;
}else{
ofLogError() << testName << " failed " << msg;
ofLogError() << "value1 " << t1;
ofLogError() << "value2 " << t2;
ofLogError() << file << ": " << line;
numTestsFailed++;
}
}

template<typename T1, typename T2>
void test_eq(T1 t1, T2 t2, const std::string & testName, const std::string & file, int line){
test_eq(t1,t2,testName,"",file,line);
}

private:
int numTestsTotal = 0;
int numTestsPassed = 0;
int numTestsFailed = 0;
};

#define test(x, ...) this->test(x,__VA_ARGS__,__FILE__,__LINE__)
#define test_eq(x, ...) this->test_eq(x,__VA_ARGS__,__FILE__,__LINE__)
21 changes: 13 additions & 8 deletions libs/openFrameworks/utils/ofFileUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,11 @@ string ofDirectory::path() const {

//------------------------------------------------------------------------------------------------------------
string ofDirectory::getAbsolutePath() const {
return std::filesystem::absolute(myDir).string();
try{
return std::filesystem::canonical(std::filesystem::absolute(myDir)).string();
}catch(...){
return std::filesystem::absolute(myDir).string();
}
}

//------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -1252,7 +1256,7 @@ bool ofDirectory::doesDirectoryExist(const std::string& _dirPath, bool bRelative
if(bRelativeToData){
dirPath = ofToDataPath(dirPath);
}
return std::filesystem::exists(dirPath);
return std::filesystem::exists(dirPath) && std::filesystem::is_directory(dirPath);
}

//------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -1420,12 +1424,13 @@ bool ofFilePath::createEnclosingDirectory(const std::string& filePath, bool bRel
string ofFilePath::getAbsolutePath(const std::string& _path, bool bRelativeToData){
std::string path = _path;
if(bRelativeToData){
path = ofToDataPath(path);
}
try{
return std::filesystem::canonical(path).string();
}catch(...){
return path;
return ofToDataPath(path, true);
}else{
try{
return std::filesystem::canonical(std::filesystem::absolute(path)).string();
}catch(...){
return std::filesystem::absolute(path).string();
}
}
}

Expand Down
7 changes: 6 additions & 1 deletion libs/openFrameworks/utils/ofUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,12 @@ string ofToDataPath(const string& path, bool makeAbsolute){
// finally, if we do want an absolute path and we don't already have one
if (makeAbsolute) {
// then we return the absolute form of the path
return std::filesystem::absolute(outputPath).string();
auto p = std::filesystem::absolute(outputPath);
if(std::filesystem::exists(p)){
return std::filesystem::canonical(p).string();
}else{
return p.string();
}
} else {
// or output the relative path
return outputPath.string();
Expand Down
23 changes: 15 additions & 8 deletions scripts/ci/linux/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@ echo "PLATFORM_CFLAGS += $CUSTOMFLAGS" >> libs/openFrameworksCompiled/project/li
cd libs/openFrameworksCompiled/project
make Debug

echo "Building emptyExample"
cd $ROOT
cp scripts/linux/template/linux64/Makefile examples/empty/emptyExample/
cp scripts/linux/template/linux64/config.make examples/empty/emptyExample/
cd examples/empty/emptyExample
# this is not even necessary if we include that in the default.mk above
# echo "PROJECT_CFLAGS = $CUSTOMFLAGS" >> config.make
make Debug
echo "Unit tests"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arturoc why did you delete this part? I don't think a CI build is really sensible if we can't even build the emptyexample. Even if we don't want to build that temporarily, why delete this and not simply comment it out and adding some explanation?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

building empty example makes the build crash and my suspicion is that this is happening because empty example is including ofMain.h which is huge. i've changed it to build the tests which are much lighter because they only include the headers that are needed and it seems to work better.

the tests are also normal applications so if those are building emptyExample should build too.

empty example is also building in osx so we will catch most possible errors affecting emptyExample but not the tests there

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

building empty example makes the build crash

unit tests also make the build crash, so you just replaced one build-crashing application with another...

cd $ROOT/tests
for group in *; do
if [ -d $group ]; then
for test in $group/*; do
if [ -d $test ]; then
cd $test
cp ../../../scripts/linux/template/linux/Makefile .
cp ../../../scripts/linux/template/linux/config.make .
make Debug
make RunDebug
fi
done
fi
done
16 changes: 16 additions & 0 deletions scripts/ci/osx/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,19 @@ set -ev
echo "Building openFrameworks - OSX Template Project"
ROOT=${TRAVIS_BUILD_DIR:-"$( cd "$(dirname "$0")/../../.." ; pwd -P )"}
xcodebuild -configuration Release -target emptyExample -project "$ROOT/scripts/osx/template/emptyExample.xcodeproj"

echo "Unit tests"
cd $ROOT/tests
for group in *; do
if [ -d $group ]; then
for test in $group/*; do
if [ -d $test ]; then
cd $test
cp ../../../scripts/osx/template/Makefile .
cp ../../../scripts/osx/template/config.make .
make Debug
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arturoc wouldn't it be better to also use xcodebuild for this (or make for the main compilation) to have homogenity in the build system per platform?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, this compiles OF library twice, once with xcode, and once with make, see https://travis-ci.org/openframeworks/openFrameworks/jobs/75829657#L984

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i know, i'll change it when i have time but if i use xcodebuild i need to compile the PG, generate the project for the tests and then compile them so it's not trivial. i'll probably move the PG generation here if i finally do it like that

make RunDebug
fi
done
fi
done
7 changes: 7 additions & 0 deletions scripts/dev/create_package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -350,23 +350,30 @@ function createPackage {
#delete ofxAndroid in non android
if [ "$pkg_platform" != "android" ]; then
rm -Rf ofxAndroid
rm -Rf ofxUnitTests
fi
#delete ofxiPhone in non ios
if [ "$pkg_platform" != "ios" ]; then
rm -Rf ofxiPhone
rm -Rf ofxiOS
rm -Rf ofxUnitTests
fi

#delete ofxMultiTouch & ofxAccelerometer in non mobile
if [ "$pkg_platform" != "android" ] && [ "$pkg_platform" != "ios" ]; then
rm -Rf ofxMultiTouch
rm -Rf ofxAccelerometer
rm -Rf ofxUnitTests
fi

if [ "$pkg_platform" == "ios" ] || [ "$pkg_platform" == "android" ]; then
rm -Rf ofxVectorGraphics
rm -Rf ofxKinect
rm -Rf ofxUnitTests
fi

#delete unit tests by now
rm -Rf ${pkg_root}/tests

#delete eclipse projects
if [ "$pkg_platform" != "android" ] && [ "$pkg_platform" != "linux" ] && [ "$pkg_platform" != "linux64" ] && [ "$pkg_platform" != "linuxarmv6l" ] && [ "$pkg_platform" != "linuxarmv7l" ]; then
Expand Down
61 changes: 61 additions & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#########################
# core examples
#########################

*/*/bin/*
!*/*/bin/data/

#########################
# IDE
#########################

# XCode
/*/*/*.xcodeproj
/*/*/Project.xcconfig
/*/*/openFrameworks-Info.plist
/*/*/ofxiOS-Info.plist
/*/*/ofxiOS_Prefix.pch
/*/*/*/*/Default*.png
/*/*/*/*/Icon*.png

# Code::Blocks
/*/*/*.cbp
/*/*/*.workspace

# Visual Studio
/*/*/*.sln
/*/*/*.vcxproj
/*/*/*.vcxproj.user
/*/*/*.vcxproj.filters
/*/*/icon.rc

# Eclipse
/*/*/.cproject
/*/*/.project
/*/*/.settings/

#########################
# operating system
#########################

# Linux
/*/*/Makefile
/*/*/config.make
# Leave Android files in until project generation works
!/android/*/Makefile
!/android/*/config.make

# Android
android/*/test link
android/*/gen
android/*/res/raw
libOFAndroidApp*.so
gdbserver
gdb.setup
libneondetection.so
Application.mk
Android.mk

!/android/*/.cproject
!/android/*/.project
!/android/*/.settings
1 change: 1 addition & 0 deletions tests/utils/fileUtils/addons.make
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ofxUnitTests
Empty file.
Loading