diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index e670c64c..fd7af87c 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -11,7 +11,7 @@ stages: gcc-c++ libuuid-devel parted-devel gtkmm30-devel make polkit file # Extra packages only needed during the test stage. - - yum install -y e2fsprogs + - yum install -y e2fsprogs xorg-x11-server-Xvfb - cat /etc/os-release .ubuntu_image_template: &ubuntu_image_definition @@ -23,7 +23,7 @@ stages: uuid-dev libparted-dev libgtkmm-3.0-dev make policykit-1 # Extra packages only needed during the test stage. - - apt-get install -y e2fsprogs + - apt-get install -y e2fsprogs xvfb - cat /etc/os-release .build_stage_template: &build_stage_definition diff --git a/tests/test_ext2.cc b/tests/test_ext2.cc index f3da218c..9a0421b7 100644 --- a/tests/test_ext2.cc +++ b/tests/test_ext2.cc @@ -43,6 +43,7 @@ #include #include +#include #include #include #include @@ -203,6 +204,38 @@ TEST_F(ext2Test, Create) } // namespace GParted +// Re-execute current executable using xvfb-run so that it provides a virtual X11 display. +void exec_using_xvfb_run(int argc, char** argv) +{ + // argc+2 = Space for "xvfb-run" command, existing argc strings plus NULL pointer. + size_t size = sizeof(char*) * (argc+2); + char** new_argv = (char**)malloc(size); + if (new_argv == NULL) + { + fprintf(stderr, "Failed to allocate %lu bytes of memory. errno=%d,%s\n", + (unsigned long)size, errno, strerror(errno)); + exit(EXIT_FAILURE); + } + + new_argv[0] = strdup("xvfb-run"); + if (new_argv[0] == NULL) + { + fprintf(stderr, "Failed to allocate %lu bytes of memory. errno=%d,%s\n", + (unsigned long)strlen(new_argv[0])+1, errno, strerror(errno)); + exit(EXIT_FAILURE); + } + + // Copy argv pointers including final NULL pointer. + for (unsigned int i = 0; i <= (unsigned)argc; i++) + new_argv[i+1] = argv[i]; + + execvp(new_argv[0], new_argv); + fprintf(stderr, "Failed to execute '%s %s ...'. errno=%d,%s\n", new_argv[0], new_argv[1], + errno, strerror(errno)); + exit(EXIT_FAILURE); +} + + // Custom Google Test main(). // Reference: // * Google Test, Primer, Writing the main() function @@ -210,6 +243,15 @@ TEST_F(ext2Test, Create) int main(int argc, char** argv) { printf("Running main() from %s\n", __FILE__); + + const char* display = getenv("DISPLAY"); + if (display == NULL) + { + printf("DISPLAY environment variable unset. Executing 'xvfb-run %s ...'\n", argv[0]); + exec_using_xvfb_run(argc, argv); + } + printf("DISPLAY=\"%s\"\n", display); + testing::InitGoogleTest(&argc, argv); // Initialise threading in GParted to allow FileSystem interface classes to