github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/git/repo_attribute_test.go (about)

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