Initial version of "scan tests/ for test suites" functionality.
authorMarkus Kauppila
Tue, 07 Jun 2011 21:11:11 +0300
changeset 5689f1ab815aa4af
parent 5688 bbc19d6aaddb
child 5690 82da33693536
Initial version of "scan tests/ for test suites" functionality.
test/test-automation/runner.c
     1.1 --- a/test/test-automation/runner.c	Mon Jun 06 21:16:18 2011 +0300
     1.2 +++ b/test/test-automation/runner.c	Tue Jun 07 21:11:11 2011 +0300
     1.3 @@ -24,6 +24,7 @@
     1.4  #include <stdlib.h>
     1.5  #include <unistd.h>
     1.6  #include <string.h>
     1.7 +#include <dirent.h>
     1.8  
     1.9  #include <sys/types.h>
    1.10  
    1.11 @@ -45,33 +46,78 @@
    1.12  static int only_selected_suite = 0;
    1.13  
    1.14  //<! Size of the test and suite name buffers
    1.15 -#define NAME_BUFFER_SIZE 256
    1.16 +#define NAME_BUFFER_SIZE 1024
    1.17  //!< Name of the selected test
    1.18  char selected_test_name[NAME_BUFFER_SIZE];
    1.19  //!< Name of the selected suite
    1.20  char selected_suite_name[NAME_BUFFER_SIZE];
    1.21  
    1.22 -//!< Temporary array to hold test suite names
    1.23 -#if defined(linux) || defined( __linux)
    1.24 -	char *testSuites[] = { "tests/libtestdummy.so", "tests/libtestrect.so", NULL};
    1.25 -#else
    1.26 -	char *testSuites[] = { "tests/libtestdummy.dylib", "tests/libtestrect.dylib", NULL};
    1.27 -#endif
    1.28  
    1.29 +/*!
    1.30 + * Holds information about test suite. Implemented as
    1.31 + * linked list. \todo write better doc
    1.32 + */
    1.33 +typedef struct TestSuiteReference {
    1.34 +	char *name; //<! test suite name
    1.35 +	struct TestSuiteReference *next; //!< Pointer to next item in the list
    1.36 +} TestSuiteReference;
    1.37 +
    1.38 +static TestSuiteReference *suites = NULL;
    1.39  
    1.40  /*!
    1.41 - * Returns the name for the dynamic library
    1.42 - * which implements the test suite.
    1.43 + * Scans the tests/ directory and returns the names
    1.44 + * of the dynamic libraries implementing the test suites.
    1.45 + * Note: currently function assumes that test suites names
    1.46 + * are in following format: libtestsuite.dylib or libtestsuite.so.
    1.47   *
    1.48 - * (in the future: scans the test/ directory and
    1.49 - * returns the names of the dynamic libraries
    1.50 - * implementing the test suites)
    1.51 - *
    1.52 - * \return Array of test suite names
    1.53 + * \return Pointer to TestSuiteReference which holds all the info about suites
    1.54   */
    1.55 -char **
    1.56 -ScanForTestSuites() {
    1.57 -	return testSuites;
    1.58 +TestSuiteReference *
    1.59 +ScanForTestSuites(/*char *directoryName*/) {
    1.60 +	typedef struct dirent Entry;
    1.61 +	DIR *directory = opendir("tests/");
    1.62 +
    1.63 +	TestSuiteReference *suites = NULL;
    1.64 +
    1.65 +	Entry *entry = NULL;
    1.66 +	if(directory) {
    1.67 +		while(entry = readdir(directory)) {
    1.68 +			if(entry->d_namlen > 2) { // discards . and ..
    1.69 +				const int bufferSize = 1024;
    1.70 +				char buffer[bufferSize];
    1.71 +				memset(buffer, 0, bufferSize);
    1.72 +
    1.73 +				strcat(buffer, "tests/"); // \todo convert to define or something
    1.74 +
    1.75 +				char *name = strtok(entry->d_name, ".");
    1.76 +				char *extension = strtok(NULL, ".");
    1.77 +				if(strcmp(extension, "dylib") == 0 || strcmp(extension, "so") == 0) {
    1.78 +					strcat(buffer, name);
    1.79 +					strcat(buffer, ".");
    1.80 +					strcat(buffer, extension);
    1.81 +
    1.82 +					TestSuiteReference *reference = (TestSuiteReference *) SDL_malloc(sizeof(TestSuiteReference));
    1.83 +					memset(reference, 0, sizeof(TestSuiteReference));
    1.84 +
    1.85 +					int length = strlen(buffer) + 1; // + 1 for '\0'?
    1.86 +					reference->name = SDL_malloc(length * sizeof(char));
    1.87 +
    1.88 +					strcpy(reference->name, buffer);
    1.89 +					reference->next = suites;
    1.90 +
    1.91 +					suites = reference;
    1.92 +
    1.93 +					// printf("Reference added to: %s\n", buffer)
    1.94 +				}
    1.95 +			}
    1.96 +		}
    1.97 +
    1.98 +		closedir(directory);
    1.99 +	} else {
   1.100 +		perror("Couldn't open directory: tests/");
   1.101 +	}
   1.102 +
   1.103 +	return suites;
   1.104  }
   1.105  
   1.106  
   1.107 @@ -381,9 +427,12 @@
   1.108  	int suiteCounter = 0;
   1.109  
   1.110  	const Uint32 startTicks = SDL_GetTicks();
   1.111 -	char **testSuiteNames = ScanForTestSuites();
   1.112 +	TestSuiteReference *suites = ScanForTestSuites();
   1.113  
   1.114 -	for(testSuiteName = testSuiteNames[suiteCounter]; testSuiteName; testSuiteName = testSuiteNames[++suiteCounter]) {
   1.115 +	TestSuiteReference *suiteReference = NULL;
   1.116 +	for(suiteReference = suites; suiteReference; suiteReference = suiteReference->next) {
   1.117 +		char *testSuiteName = suiteReference->name;
   1.118 +
   1.119  		// if the current suite isn't selected, go to next suite
   1.120  		if(SuiteIsSelected(testSuiteName)) {
   1.121  			void *suite = LoadTestSuite(testSuiteName);