1// Copyright 2021 The Gitea Authors. All rights reserved.
2// Use of this source code is governed by a MIT-style
3// license that can be found in the LICENSE file.
4
5package git
6
7import (
8	"testing"
9	"time"
10
11	"github.com/stretchr/testify/assert"
12)
13
14func Test_nulSeparatedAttributeWriter_ReadAttribute(t *testing.T) {
15	wr := &nulSeparatedAttributeWriter{
16		attributes: make(chan attributeTriple, 5),
17	}
18
19	testStr := ".gitignore\"\n\x00linguist-vendored\x00unspecified\x00"
20
21	n, err := wr.Write([]byte(testStr))
22
23	assert.Equal(t, n, len(testStr))
24	assert.NoError(t, err)
25	select {
26	case attr := <-wr.ReadAttribute():
27		assert.Equal(t, ".gitignore\"\n", attr.Filename)
28		assert.Equal(t, "linguist-vendored", attr.Attribute)
29		assert.Equal(t, "unspecified", attr.Value)
30	case <-time.After(100 * time.Millisecond):
31		assert.Fail(t, "took too long to read an attribute from the list")
32	}
33	// Write a second attribute again
34	n, err = wr.Write([]byte(testStr))
35
36	assert.Equal(t, n, len(testStr))
37	assert.NoError(t, err)
38
39	select {
40	case attr := <-wr.ReadAttribute():
41		assert.Equal(t, ".gitignore\"\n", attr.Filename)
42		assert.Equal(t, "linguist-vendored", attr.Attribute)
43		assert.Equal(t, "unspecified", attr.Value)
44	case <-time.After(100 * time.Millisecond):
45		assert.Fail(t, "took too long to read an attribute from the list")
46	}
47
48	//Write a partial attribute
49	_, err = wr.Write([]byte("incomplete-file"))
50	assert.NoError(t, err)
51	_, err = wr.Write([]byte("name\x00"))
52	assert.NoError(t, err)
53
54	select {
55	case <-wr.ReadAttribute():
56		assert.Fail(t, "There should not be an attribute ready to read")
57	case <-time.After(100 * time.Millisecond):
58	}
59	_, err = wr.Write([]byte("attribute\x00"))
60	assert.NoError(t, err)
61	select {
62	case <-wr.ReadAttribute():
63		assert.Fail(t, "There should not be an attribute ready to read")
64	case <-time.After(100 * time.Millisecond):
65	}
66
67	_, err = wr.Write([]byte("value\x00"))
68	assert.NoError(t, err)
69
70	attr := <-wr.ReadAttribute()
71	assert.Equal(t, "incomplete-filename", attr.Filename)
72	assert.Equal(t, "attribute", attr.Attribute)
73	assert.Equal(t, "value", attr.Value)
74
75	_, err = wr.Write([]byte("shouldbe.vendor\x00linguist-vendored\x00set\x00shouldbe.vendor\x00linguist-generated\x00unspecified\x00shouldbe.vendor\x00linguist-language\x00unspecified\x00"))
76	assert.NoError(t, err)
77	attr = <-wr.ReadAttribute()
78	assert.NoError(t, err)
79	assert.EqualValues(t, attributeTriple{
80		Filename:  "shouldbe.vendor",
81		Attribute: "linguist-vendored",
82		Value:     "set",
83	}, attr)
84	attr = <-wr.ReadAttribute()
85	assert.NoError(t, err)
86	assert.EqualValues(t, attributeTriple{
87		Filename:  "shouldbe.vendor",
88		Attribute: "linguist-generated",
89		Value:     "unspecified",
90	}, attr)
91	attr = <-wr.ReadAttribute()
92	assert.NoError(t, err)
93	assert.EqualValues(t, attributeTriple{
94		Filename:  "shouldbe.vendor",
95		Attribute: "linguist-language",
96		Value:     "unspecified",
97	}, attr)
98}
99
100func Test_lineSeparatedAttributeWriter_ReadAttribute(t *testing.T) {
101	wr := &lineSeparatedAttributeWriter{
102		attributes: make(chan attributeTriple, 5),
103	}
104
105	testStr := `".gitignore\"\n": linguist-vendored: unspecified
106`
107	n, err := wr.Write([]byte(testStr))
108
109	assert.Equal(t, n, len(testStr))
110	assert.NoError(t, err)
111
112	select {
113	case attr := <-wr.ReadAttribute():
114		assert.Equal(t, ".gitignore\"\n", attr.Filename)
115		assert.Equal(t, "linguist-vendored", attr.Attribute)
116		assert.Equal(t, "unspecified", attr.Value)
117	case <-time.After(100 * time.Millisecond):
118		assert.Fail(t, "took too long to read an attribute from the list")
119	}
120
121	// Write a second attribute again
122	n, err = wr.Write([]byte(testStr))
123
124	assert.Equal(t, n, len(testStr))
125	assert.NoError(t, err)
126
127	select {
128	case attr := <-wr.ReadAttribute():
129		assert.Equal(t, ".gitignore\"\n", attr.Filename)
130		assert.Equal(t, "linguist-vendored", attr.Attribute)
131		assert.Equal(t, "unspecified", attr.Value)
132	case <-time.After(100 * time.Millisecond):
133		assert.Fail(t, "took too long to read an attribute from the list")
134	}
135
136	//Write a partial attribute
137	_, err = wr.Write([]byte("incomplete-file"))
138	assert.NoError(t, err)
139	_, err = wr.Write([]byte("name: "))
140	assert.NoError(t, err)
141	select {
142	case <-wr.ReadAttribute():
143		assert.Fail(t, "There should not be an attribute ready to read")
144	case <-time.After(100 * time.Millisecond):
145	}
146	_, err = wr.Write([]byte("attribute: "))
147	assert.NoError(t, err)
148	select {
149	case <-wr.ReadAttribute():
150		assert.Fail(t, "There should not be an attribute ready to read")
151	case <-time.After(100 * time.Millisecond):
152	}
153	_, err = wr.Write([]byte("value\n"))
154	assert.NoError(t, err)
155	attr := <-wr.ReadAttribute()
156	assert.Equal(t, "incomplete-filename", attr.Filename)
157	assert.Equal(t, "attribute", attr.Attribute)
158	assert.Equal(t, "value", attr.Value)
159}
160