• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..26-Aug-2021-

model/H26-Aug-2021-978772

testdata/H26-Aug-2021-176124

autorest.goH A D26-Aug-20213.6 KiB145106

autorestMetadata.goH A D26-Aug-20212.1 KiB8161

changelog.goH A D26-Aug-20215.3 KiB183140

format.goH A D26-Aug-2021440 2013

generation.goH A D26-Aug-20219.6 KiB301230

generationMetadata.goH A D26-Aug-20214.8 KiB13493

readmeFiles.goH A D26-Aug-20212.1 KiB7458

readmeFiles_test.goH A D26-Aug-20211.6 KiB6758

validate.goH A D26-Aug-2021840 2918

readmeFiles.go

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License. See License.txt in the project root for license information.
3
4package autorest
5
6import (
7	"fmt"
8	"io"
9	"io/ioutil"
10	"log"
11	"regexp"
12	"strings"
13)
14
15// ReadBatchTags reads from a io.Reader of readme.go.md, parses the `multiapi` section and produces a slice of tags
16func ReadBatchTags(reader io.Reader) ([]string, error) {
17	b, err := ioutil.ReadAll(reader)
18	if err != nil {
19		return nil, err
20	}
21
22	lines := strings.Split(string(b), "\n")
23
24	start := -1
25	end := -1
26	for i, line := range lines {
27		if multiAPISectionBeginRegex.MatchString(line) {
28			if start >= 0 {
29				return nil, fmt.Errorf("multiple multiapi section found on line %d and %d, we should only have one", start+1, i+1)
30			}
31			start = i
32		}
33		if start >= 0 && end < 0 && multiAPISectionEndRegex.MatchString(line) {
34			end = i
35		}
36	}
37
38	if start < 0 {
39		return nil, fmt.Errorf("cannot find multiapi section")
40	}
41	if end < 0 {
42		return nil, fmt.Errorf("multiapi section does not properly end")
43	}
44
45	// get the content of the mutliapi section
46	multiAPISection := lines[start+1 : end]
47	// the multiapi section should at least have two lines
48	if len(multiAPISection) < 2 {
49		return nil, fmt.Errorf("multiapi section cannot be parsed")
50	}
51	// verify the first line of the section should be "batch:"
52	if !batchRegex.MatchString(multiAPISection[0]) {
53		return nil, fmt.Errorf("multiapi section should begin with `batch:`")
54	}
55	// iterate over the rest lines, should one for one tag
56	var tags []string
57	for _, line := range multiAPISection[1:] {
58		matches := tagRegex.FindStringSubmatch(line)
59		if len(matches) < 2 {
60			log.Printf("[WARNING] line in batch '%s' does not starts with 'tags', ignore", line)
61			continue
62		}
63		tags = append(tags, matches[1])
64	}
65	return tags, nil
66}
67
68var (
69	multiAPISectionBeginRegex = regexp.MustCompile("^```\\s*yaml\\s*\\$\\(go\\)\\s*&&\\s*\\$\\(multiapi\\)")
70	multiAPISectionEndRegex   = regexp.MustCompile("^\\s*```\\s*$")
71	batchRegex                = regexp.MustCompile(`^\s*batch:\s*$`)
72	tagRegex                  = regexp.MustCompile(`^\s*-\s+tag: (\S+)\s*$`)
73)
74

readmeFiles_test.go

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License. See License.txt in the project root for license information.
3
4package autorest_test
5
6import (
7	"os"
8	"reflect"
9	"testing"
10
11	"github.com/Azure/azure-sdk-for-go/tools/generator/autorest"
12)
13
14func TestReadBatchTags(t *testing.T) {
15	testdata := []struct {
16		readmePath string
17		expected   []string
18		err        string
19	}{
20		{
21			readmePath: "./testdata/proper_readme.go.md",
22			expected: []string{
23				"package-2020-01",
24				"package-2019-01-preview",
25			},
26		},
27		{
28			readmePath: "./testdata/multiple_multiapi_readme.go.md",
29			err:        "multiple multiapi section found on line 14 and 19, we should only have one",
30		},
31		{
32			readmePath: "./testdata/missing_multiapi_readme.go.md",
33			err:        "cannot find multiapi section",
34		},
35		{
36			readmePath: "./testdata/too_short_readme.go.md",
37			err:        "multiapi section cannot be parsed",
38		},
39		{
40			readmePath: "./testdata/missing_batch_readme.go.md",
41			err:        "multiapi section should begin with `batch:`",
42		},
43	}
44
45	for _, c := range testdata {
46		t.Logf("Testing %s", c.readmePath)
47		reader, err := os.Open(c.readmePath)
48		if err != nil {
49			t.Fatalf("unexpected error when opening readme file: %+v", err)
50		}
51
52		tags, err := autorest.ReadBatchTags(reader)
53		if err != nil {
54			if c.expected != nil {
55				t.Fatalf("unexpected error: %+v", err)
56			}
57			if err.Error() != c.err {
58				t.Fatalf("expected error message '%s', but got error message '%s'", c.err, err.Error())
59			}
60		} else {
61			if !reflect.DeepEqual(c.expected, tags) {
62				t.Fatalf("expected %+v, but got %+v", c.expected, tags)
63			}
64		}
65	}
66}
67