github.com/goreleaser/goreleaser@v1.25.1/pkg/config/config_slack_test.go (about)

     1  package config
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"errors"
     7  	"io"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestUnmarshalSlackBlocks(t *testing.T) {
    14  	t.Parallel()
    15  
    16  	t.Run("valid blocks", func(t *testing.T) {
    17  		t.Parallel()
    18  
    19  		prop, err := LoadReader(goodBlocksSlackConf())
    20  		require.NoError(t, err)
    21  
    22  		expectedBlocks := []SlackBlock{
    23  			{
    24  				Internal: map[string]interface{}{
    25  					"type": "header",
    26  					"text": map[string]interface{}{
    27  						"type": "plain_text",
    28  						"text": "{{ .Version }}",
    29  					},
    30  				},
    31  			},
    32  			{
    33  				Internal: map[string]interface{}{
    34  					"text": map[string]interface{}{
    35  						"type": "mrkdwn",
    36  						"text": "Heading\n=======\n\n**Bold**\n",
    37  					},
    38  					"type": "section",
    39  				},
    40  			},
    41  		}
    42  		// assert Unmarshal from YAML
    43  		require.Equal(t, expectedBlocks, prop.Announce.Slack.Blocks)
    44  
    45  		jazon, err := json.Marshal(prop.Announce.Slack.Blocks)
    46  		require.NoError(t, err)
    47  
    48  		var untyped []SlackBlock
    49  		require.NoError(t, json.Unmarshal(jazon, &untyped))
    50  
    51  		// assert that JSON Marshal didn't alter the struct
    52  		require.Equal(t, expectedBlocks, prop.Announce.Slack.Blocks)
    53  	})
    54  
    55  	t.Run("invalid blocks", func(t *testing.T) {
    56  		t.Parallel()
    57  
    58  		_, err := LoadReader(badBlocksSlackConf())
    59  		require.Error(t, err)
    60  	})
    61  }
    62  
    63  func TestUnmarshalSlackAttachments(t *testing.T) {
    64  	t.Parallel()
    65  
    66  	t.Run("valid attachments", func(t *testing.T) {
    67  		t.Parallel()
    68  
    69  		prop, err := LoadReader(goodAttachmentsSlackConf())
    70  		require.NoError(t, err)
    71  
    72  		expectedAttachments := []SlackAttachment{
    73  			{
    74  				Internal: map[string]interface{}{
    75  					"color": "#46a64f",
    76  					"fields": []interface{}{
    77  						map[string]interface{}{
    78  							"short": false,
    79  							"title": "field 1",
    80  							"value": "value 1",
    81  						},
    82  					},
    83  					"footer": "a footer",
    84  					"mrkdwn_in": []interface{}{
    85  						"text",
    86  					},
    87  					"pretext": "optional",
    88  					"text":    "another",
    89  					"title":   "my_title",
    90  				},
    91  			},
    92  		}
    93  		// assert Unmarshal from YAML
    94  		require.Equal(t, expectedAttachments, prop.Announce.Slack.Attachments)
    95  
    96  		jazon, err := json.Marshal(prop.Announce.Slack.Attachments)
    97  		require.NoError(t, err)
    98  
    99  		var untyped []SlackAttachment
   100  		require.NoError(t, json.Unmarshal(jazon, &untyped))
   101  
   102  		// assert that JSON Marshal didn't alter the struct
   103  		require.Equal(t, expectedAttachments, prop.Announce.Slack.Attachments)
   104  	})
   105  
   106  	t.Run("invalid attachments", func(t *testing.T) {
   107  		t.Parallel()
   108  
   109  		_, err := LoadReader(badAttachmentsSlackConf())
   110  		require.Error(t, err)
   111  	})
   112  }
   113  
   114  func TestUnmarshalYAMLSlackBlocks(t *testing.T) {
   115  	// func (a *SlackAttachment) UnmarshalYAML(unmarshal func(interface{}) error) error {
   116  	t.Parallel()
   117  
   118  	const testError = "testError"
   119  	erf := func(_ interface{}) error {
   120  		return errors.New(testError)
   121  	}
   122  
   123  	t.Run("SlackBlock.UnmarshalYAML error case", func(t *testing.T) {
   124  		t.Parallel()
   125  
   126  		var block SlackBlock
   127  		err := block.UnmarshalYAML(erf)
   128  		require.Error(t, err)
   129  		require.ErrorContains(t, err, testError)
   130  	})
   131  
   132  	t.Run("SlackAttachment.UnmarshalYAML error case", func(t *testing.T) {
   133  		t.Parallel()
   134  
   135  		var attachment SlackAttachment
   136  		err := attachment.UnmarshalYAML(erf)
   137  		require.Error(t, err)
   138  		require.ErrorContains(t, err, testError)
   139  	})
   140  }
   141  
   142  func goodBlocksSlackConf() io.Reader {
   143  	const conf = `
   144  announce:
   145    slack:
   146      enabled: true
   147      username: my_user
   148      message_template: fallback
   149      channel: my_channel
   150      blocks:
   151        - type: header
   152          text:
   153            type: plain_text
   154            text: '{{ .Version }}'
   155        - type: section
   156          text:
   157            type: mrkdwn
   158            text: |
   159              Heading
   160              =======
   161  
   162              **Bold**
   163  `
   164  
   165  	buf := bytes.NewBufferString(conf)
   166  
   167  	return bytes.NewReader(bytes.ReplaceAll(buf.Bytes(), []byte("\t"), []byte("    ")))
   168  }
   169  
   170  func badBlocksSlackConf() io.Reader {
   171  	const conf = `
   172  announce:
   173    slack:
   174      enabled: true
   175      username: my_user
   176      message_template: fallback
   177      channel: my_channel
   178      blocks:
   179        type: header
   180          text:
   181            type: plain_text
   182            text: '{{ .Version }}'
   183        type: section
   184          text:
   185            type: mrkdwn
   186            text: |
   187              **Bold**
   188  `
   189  
   190  	buf := bytes.NewBufferString(conf)
   191  
   192  	return bytes.NewReader(bytes.ReplaceAll(buf.Bytes(), []byte("\t"), []byte("    ")))
   193  }
   194  
   195  func goodAttachmentsSlackConf() io.Reader {
   196  	const conf = `
   197  announce:
   198    slack:
   199      enabled: true
   200      username: my_user
   201      message_template: fallback
   202      channel: my_channel
   203      attachments:
   204        - mrkdwn_in: ["text"]
   205          color: '#46a64f'
   206          pretext: optional
   207          title: my_title
   208          text: another
   209          fields:
   210            - title: field 1
   211              value: value 1
   212              short: false
   213          footer: a footer
   214  `
   215  
   216  	buf := bytes.NewBufferString(conf)
   217  
   218  	return bytes.NewReader(bytes.ReplaceAll(buf.Bytes(), []byte("\t"), []byte("    ")))
   219  }
   220  
   221  func badAttachmentsSlackConf() io.Reader {
   222  	const conf = `
   223  announce:
   224    slack:
   225      enabled: true
   226      username: my_user
   227      message_template: fallback
   228      channel: my_channel
   229      attachments:
   230        key:
   231          mrkdwn_in: ["text"]
   232          color: #46a64f
   233          pretext: optional
   234          title: my_title
   235          text: another
   236          fields:
   237            - title: field 1
   238              value: value 1
   239              short: false
   240          footer: a footer
   241  `
   242  
   243  	buf := bytes.NewBufferString(conf)
   244  
   245  	return bytes.NewReader(bytes.ReplaceAll(buf.Bytes(), []byte("\t"), []byte("    ")))
   246  }