15 * An insecure implementation of mkstemp(), for those platforms that
18 int mkstemp(char *template)
32 fd=open(s,O_RDWR|O_CREAT|O_EXCL,0600);
43 #endif /* !HAVE_MKSTEMP */
46 * As write(), but always convert NL to CR NL.
48 static size_t write_text(int fd,const char *buf,size_t count)
56 if (_setmode(fd,_O_BINARY)<0)
71 if (putc('\r',fp)==EOF)
76 if (putc(buf[i],fp)==EOF)
88 * Return the length (in bytes) or any common prefix between s1 and s2.
90 size_t common_prefix_length(const char *s1,const char *s2)
93 for(i=0;s1[i] && s2[i] && s1[i]==s2[i];i++)
99 * Run a testcase, returning FALSE on fail or error and
100 * TRUE on pass or expected-fail.
101 * Suitable message(s) will be printed in all cases.
103 gboolean testcase_run(Testcase *testcase)
106 int fd,exit_status,col;
107 size_t n,pos,offset,header_len;
109 char input[]="TEST-XXXXXX";
112 GString *expected,*report;
116 n=strlen(testcase->input);
119 if (n && write_text(fd,testcase->input,n)!=n)
127 command[0]=getenv("BOOKLOUPE");
129 command[0]="." G_DIR_SEPARATOR_S "bookloupe";
132 if (testcase->expected)
133 r=spawn_sync(command,&output,&exit_status);
136 r=spawn_sync(command,NULL,&exit_status);
142 if (testcase->expected)
144 expected=g_string_new("\n\nFile: ");
145 g_string_append(expected,input);
146 g_string_append(expected,"\n\n\n");
147 header_len=expected->len;
148 g_string_append(expected,testcase->expected);
155 if (expected && strcmp(output,expected->str))
157 fprintf(stderr,"%s: FAIL\n",testcase->basename);
158 offset=common_prefix_length(output,expected->str);
159 if (offset==header_len && !output[offset])
160 fprintf(stderr,"Unexpected zero warnings from bookloupe.\n");
163 endp=strchr(output+offset,'\n');
165 endp=output+strlen(output);
166 report=g_string_new(NULL);
167 g_string_append_len(report,output,endp-output);
168 bol=strrchr(report->str,'\n');
173 col=offset-(bol-report->str);
174 fprintf(stderr,"Unexpected output from bookloupe:\n");
175 if (report->len>=header_len)
176 fprintf(stderr,"%s\n%*s^\n",report->str+header_len,col,"");
178 fprintf(stderr,"%s\n%*s^\n",report->str,col,"");
179 g_string_free(report,TRUE);
181 g_string_free(expected,TRUE);
185 g_string_free(expected,TRUE);
188 fprintf(stderr,"bookloupe exited with code %d\n",r);
190 fprintf(stderr,"%s: PASS\n",testcase->basename);
197 void testcase_free(Testcase *testcase)
199 g_free(testcase->basename);
200 g_free(testcase->input);
201 g_free(testcase->expected);