test/harness/testcaseio.c
author ali <ali@juiblex.co.uk>
Tue Sep 17 20:55:49 2013 +0100 (2013-09-17)
changeset 90 9bebf1b53a48
parent 11 4a80c6053a66
child 101 f44c530f80da
permissions -rw-r--r--
Update documentation for 2.0
ali@0
     1
#include <stdlib.h>
ali@0
     2
#include <stdio.h>
ali@0
     3
#include <string.h>
ali@6
     4
#include <glib.h>
ali@5
     5
#include <bl/bl.h>
ali@0
     6
#include "testcaseparser.h"
ali@9
     7
#include "testcaseinput.h"
ali@0
     8
#include "testcaseio.h"
ali@17
     9
#include "warningsparser.h"
ali@0
    10
ali@0
    11
/*
ali@0
    12
 * Read a testcase in from a file.
ali@11
    13
 * On error, print a suitable message using g_printerr and return NULL.
ali@0
    14
 * The returned testcase should be freed with testcase_free().
ali@0
    15
 */
ali@0
    16
Testcase *testcase_parse_file(const char *filename)
ali@0
    17
{
ali@0
    18
    Testcase *testcase;
ali@0
    19
    TestcaseParser *parser;
ali@9
    20
    TestcaseInput *input=NULL;
ali@17
    21
    GMarkupParseContext *context;
ali@17
    22
    GError *err=NULL;
ali@9
    23
    char *s,*arg;
ali@0
    24
    const char *tag,*text;
ali@6
    25
    gboolean found_tag=FALSE;
ali@0
    26
    parser=testcase_parser_new_from_file(filename);
ali@0
    27
    if (!parser)
ali@0
    28
	return NULL;
ali@0
    29
    if (!*testcase_parser_get_flag(parser))
ali@0
    30
    {
ali@11
    31
	g_printerr("%s: Not a valid testcase (flag)\n",filename);
ali@0
    32
	testcase_parser_free(parser);
ali@0
    33
	return NULL;
ali@0
    34
    }
ali@6
    35
    testcase=g_new0(Testcase,1);
ali@6
    36
    testcase->basename=g_path_get_basename(filename);
ali@0
    37
    s=strrchr(testcase->basename,'.');
ali@0
    38
    if (s)
ali@0
    39
	*s='\0';
ali@0
    40
    while(testcase_parser_get_next_tag(parser,&tag,&text))
ali@0
    41
    {
ali@9
    42
	if (!input && !strcmp(tag,"INPUT"))
ali@9
    43
	    input=testcase_input_new("TEST-XXXXXX",text);
ali@9
    44
	else if (g_str_has_prefix(tag,"INPUT(") && tag[strlen(tag)-1]==')')
ali@9
    45
	{
ali@9
    46
	    arg=g_strndup(tag+6,strlen(tag)-7);
ali@9
    47
	    s=g_path_get_dirname(arg);
ali@9
    48
	    if (strcmp(s,"."))
ali@9
    49
	    {
ali@9
    50
		/*
ali@9
    51
		 * Otherwise it would be possible to overwrite an arbitary
ali@9
    52
		 * file on somebody's computer by getting them to run a
ali@9
    53
		 * testcase!
ali@9
    54
		 */
ali@11
    55
		g_printerr(
ali@9
    56
		  "%s: Input files may not have a directory component\n",arg);
ali@9
    57
		g_free(s);
ali@9
    58
		g_free(arg);
ali@9
    59
		testcase_free(testcase);
ali@9
    60
		testcase_parser_free(parser);
ali@9
    61
		return NULL;
ali@9
    62
	    }
ali@9
    63
	    g_free(s);
ali@9
    64
	    testcase->inputs=g_slist_prepend(testcase->inputs,
ali@9
    65
	      testcase_input_new(arg,text));
ali@9
    66
	    if (!strstr(arg,"XXXXXX"))
ali@9
    67
		testcase->flags|=TESTCASE_TMP_DIR;
ali@9
    68
	    g_free(arg);
ali@9
    69
	}
ali@17
    70
	else if (!testcase->expected && !testcase->warnings &&
ali@17
    71
	  !strcmp(tag,"EXPECTED"))
ali@6
    72
	    testcase->expected=g_strdup(text);
ali@17
    73
	else if (!testcase->expected && !testcase->warnings &&
ali@17
    74
	  !strcmp(tag,"WARNINGS"))
ali@17
    75
	{
ali@17
    76
	    context=warnings_parse_context_new(testcase);
ali@17
    77
	    if (!g_markup_parse_context_parse(context,text,-1,&err) ||
ali@17
    78
	      !g_markup_parse_context_end_parse(context,&err))
ali@17
    79
	    {
ali@17
    80
		g_markup_parse_context_free(context);
ali@17
    81
		g_printerr("%s\n",err->message);
ali@17
    82
		g_clear_error(&err);
ali@17
    83
		testcase_free(testcase);
ali@17
    84
		testcase_parser_free(parser);
ali@17
    85
		return NULL;
ali@17
    86
	    }
ali@17
    87
	    g_markup_parse_context_free(context);
ali@17
    88
	}
ali@7
    89
	else if (!testcase->encoding && !strcmp(tag,"ENCODING"))
ali@8
    90
	    testcase->encoding=g_strchomp(g_strdup(text));
ali@9
    91
	else if (!testcase->encoding && !strcmp(tag,"OPTIONS"))
ali@9
    92
	{
ali@9
    93
	    testcase->options=g_strsplit(text,"\n",0);
ali@9
    94
	    g_free(testcase->options[g_strv_length(testcase->options)-1]);
ali@9
    95
	    testcase->options[g_strv_length(testcase->options)-1]=NULL;
ali@9
    96
	}
ali@0
    97
	else
ali@0
    98
	{
ali@11
    99
	    g_printerr("%s: Not a valid testcase (%s)\n",filename,tag);
ali@0
   100
	    testcase_free(testcase);
ali@0
   101
	    testcase_parser_free(parser);
ali@0
   102
	    return NULL;
ali@0
   103
	}
ali@0
   104
	found_tag=TRUE;
ali@0
   105
    }
ali@0
   106
    if (!testcase_parser_at_eof(parser))
ali@0
   107
    {
ali@0
   108
	if (found_tag)
ali@11
   109
	    g_printerr("%s: Not a valid testcase (garbage at end)\n",
ali@0
   110
	      filename);
ali@0
   111
	else
ali@11
   112
	    g_printerr("%s: Not a valid testcase (no valid tags)\n",
ali@0
   113
	      filename);
ali@0
   114
	testcase_free(testcase);
ali@0
   115
	testcase_parser_free(parser);
ali@0
   116
	return NULL;
ali@0
   117
    }
ali@9
   118
    if (!input)
ali@9
   119
	input=testcase_input_new("TEST-XXXXXX",NULL);
ali@9
   120
    testcase->inputs=g_slist_prepend(testcase->inputs,input);
ali@0
   121
    testcase_parser_free(parser);
ali@0
   122
    return testcase;
ali@0
   123
}