mirror of
https://github.com/keymanapp/keyman.git
synced 2026-08-05 16:35:33 +00:00
Had a real yak shave this morning with disabling assertions in release
builds in our C/C++ code. It turns out that our unit tests use
`assert()` which we intended to use from `test_assert.h`, but in some
cases `cassert` or `assert.h` had been #included after `test_assert.h`,
overriding our special `assert()` macro. The chain of includes is
somewhat hard to puzzle out -- it's often buried several levels deep.
This meant that a release build would drop all test assertions, meaning
most tests passed, unsurprisingly, as there were no assertions left to
fail ... but some tests failed with crashes because we optimized out
important lines such as `assert(some_important_function())`.
I was quite unhappy with this fragility, so I have opted to rename
`assert()` to `test_assert()` in all of our home-grown C/C++ unit tests,
which further highlighted unit tests which were only using the C/C++
`assert()` and not ours, so then had to figure out which unit test
executables needed to have `test_assert` added, and then ... then ...
discovered a bug in `test_color.h`, where we were #including
`io.h`/`unistd.h` inside a `namespace console_color {}` block, which
just happened to be the first ref to those beautiful headers, and thus
(because `#pragma once`) meant that useful little functions like
`access()` were no longer accessible to us in the global namespace.
I have also audited Every Single Call to `assert()` to verify that we do
not do Important Work inside the parentheses, and, apart from those
offending unit tests, now resolved with `test_assert()`, it looks like
all is good.
I would like to present one very well-shaved yak in this commit.
Fixes: #12619
83 lines
1.5 KiB
C++
83 lines
1.5 KiB
C++
/*
|
|
* Keyman is copyright (C) SIL International. MIT License.
|
|
*
|
|
* Keyman Core - test console color helpers
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <ostream>
|
|
|
|
#ifdef _MSC_VER
|
|
#include <io.h>
|
|
#else
|
|
#include <unistd.h>
|
|
#endif
|
|
|
|
namespace console_color {
|
|
|
|
enum ansi_code {
|
|
BLACK = 0,
|
|
RED = 1,
|
|
GREEN = 2,
|
|
YELLOW = 3,
|
|
BLUE = 4,
|
|
MAGENTA = 5,
|
|
CYAN = 6,
|
|
WHITE = 7,
|
|
GREY = 8,
|
|
BRIGHT_RED = 196
|
|
};
|
|
|
|
extern bool enabled;
|
|
|
|
class fg {
|
|
ansi_code code;
|
|
public:
|
|
fg(ansi_code pCode) : code(pCode) {}
|
|
|
|
inline friend std::wostream&
|
|
operator<<(std::wostream& os, const fg& mod) {
|
|
if(!enabled) return os;
|
|
return os << "\033[38;5;" << mod.code << "m";
|
|
}
|
|
};
|
|
|
|
class bg {
|
|
ansi_code code;
|
|
public:
|
|
bg(ansi_code pCode) : code(pCode) {}
|
|
|
|
inline friend std::wostream&
|
|
operator<<(std::wostream& os, const bg& mod) {
|
|
if(!enabled) return os;
|
|
return os << "\033[48;5;" << mod.code << "m";
|
|
}
|
|
};
|
|
|
|
#define __define_ansi_code__(name,value) class name { \
|
|
inline friend std::wostream& \
|
|
operator<<(std::wostream& os, const name& mod) { \
|
|
if(!enabled) return os; \
|
|
return os << "\033[" value "m"; \
|
|
} \
|
|
}
|
|
|
|
__define_ansi_code__(reset, "0");
|
|
__define_ansi_code__(bold, "1");
|
|
__define_ansi_code__(underline, "4");
|
|
__define_ansi_code__(reversed, "7");
|
|
|
|
#undef __define_ansi_code__
|
|
|
|
#ifdef _MSC_VER
|
|
inline bool isaterminal() {
|
|
return _isatty(_fileno(stdout));
|
|
}
|
|
#else
|
|
inline bool isaterminal() {
|
|
return isatty(STDOUT_FILENO);
|
|
}
|
|
#endif
|
|
|
|
}
|