File: build-oss-fuzz-corpus.go

package info (click to toggle)
golang-github-yuin-goldmark 1.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,564 kB
  • sloc: ansic: 76; makefile: 52
file content (61 lines) | stat: -rw-r--r-- 1,184 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main

import (
	"archive/zip"
	"encoding/json"
	"io/ioutil"
	"log"
	"os"
	"strconv"
	"strings"
)

type TestCase struct {
	Example  int    `json:"example"`
	Markdown string `json:"markdown"`
}

func main() {
	corpus_out := os.Args[1]
	if !strings.HasSuffix(corpus_out, ".zip") {
		log.Fatalln("Expected command line:", os.Args[0], "<corpus_output>.zip")
	}

	zip_file, err := os.Create(corpus_out)

	zip_writer := zip.NewWriter(zip_file)

	if err != nil {
		log.Fatalln("Failed creating file:", err)
	}

	json_corpus := "_test/spec.json"
	bs, err := ioutil.ReadFile(json_corpus)
	if err != nil {
		log.Fatalln("Could not open file:", json_corpus)
		panic(err)
	}
	var testCases []TestCase
	if err := json.Unmarshal(bs, &testCases); err != nil {
		panic(err)
	}

	for _, c := range testCases {
		file_in_zip := "example-" + strconv.Itoa(c.Example)
		f, err := zip_writer.Create(file_in_zip)
		if err != nil {
			log.Fatal(err)
		}
		_, err = f.Write([]byte(c.Markdown))
		if err != nil {
			log.Fatalf("Failed to write file: %s into zip file", file_in_zip)
		}
	}

	err = zip_writer.Close()
	if err != nil {
		log.Fatal("Failed to close zip writer", err)
	}

	zip_file.Close()
}