...

Source file src/github.com/yuin/goldmark/_tools/build-oss-fuzz-corpus.go

Documentation: github.com/yuin/goldmark/_tools

     1  package main
     2  
     3  import (
     4  	"archive/zip"
     5  	"encoding/json"
     6  	"io/ioutil"
     7  	"log"
     8  	"os"
     9  	"strconv"
    10  	"strings"
    11  )
    12  
    13  type TestCase struct {
    14  	Example  int    `json:"example"`
    15  	Markdown string `json:"markdown"`
    16  }
    17  
    18  func main() {
    19  	corpus_out := os.Args[1]
    20  	if !strings.HasSuffix(corpus_out, ".zip") {
    21  		log.Fatalln("Expected command line:", os.Args[0], "<corpus_output>.zip")
    22  	}
    23  
    24  	zip_file, err := os.Create(corpus_out)
    25  
    26  	zip_writer := zip.NewWriter(zip_file)
    27  
    28  	if err != nil {
    29  		log.Fatalln("Failed creating file:", err)
    30  	}
    31  
    32  	json_corpus := "_test/spec.json"
    33  	bs, err := ioutil.ReadFile(json_corpus)
    34  	if err != nil {
    35  		log.Fatalln("Could not open file:", json_corpus)
    36  		panic(err)
    37  	}
    38  	var testCases []TestCase
    39  	if err := json.Unmarshal(bs, &testCases); err != nil {
    40  		panic(err)
    41  	}
    42  
    43  	for _, c := range testCases {
    44  		file_in_zip := "example-" + strconv.Itoa(c.Example)
    45  		f, err := zip_writer.Create(file_in_zip)
    46  		if err != nil {
    47  			log.Fatal(err)
    48  		}
    49  		_, err = f.Write([]byte(c.Markdown))
    50  		if err != nil {
    51  			log.Fatalf("Failed to write file: %s into zip file", file_in_zip)
    52  		}
    53  	}
    54  
    55  	err = zip_writer.Close()
    56  	if err != nil {
    57  		log.Fatal("Failed to close zip writer", err)
    58  	}
    59  
    60  	zip_file.Close()
    61  }

View as plain text