go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/luci_notify/testutil/config.go (about)

     1  // Copyright 2017 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package testutil
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  
    21  	"github.com/golang/protobuf/proto"
    22  
    23  	notifyConfig "go.chromium.org/luci/luci_notify/api/config"
    24  )
    25  
    26  // LoadProjectConfig returns a luci-notify configuration from api/config/testdata
    27  // by filename (without extension). It does NOT validate the configuration.
    28  func LoadProjectConfig(filename string) (*notifyConfig.ProjectConfig, error) {
    29  	dir, err := os.Getwd()
    30  	if err != nil {
    31  		return nil, err
    32  	}
    33  	fullname := fmt.Sprintf("%s/../api/config/testdata/%s.cfg", dir, filename)
    34  	buf, err := os.ReadFile(fullname)
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  	return ParseProjectConfig(string(buf[:]))
    39  }
    40  
    41  // ParseProjectConfig parses a luci-notify configuration from a string. It does
    42  // NOT validate the configuration.
    43  func ParseProjectConfig(config string) (*notifyConfig.ProjectConfig, error) {
    44  	project := notifyConfig.ProjectConfig{}
    45  	if err := proto.UnmarshalText(config, &project); err != nil {
    46  		return nil, err
    47  	}
    48  	return &project, nil
    49  }
    50  
    51  // ParseSettings parses a luci-notify service configuration from a string. It does
    52  // NOT validate the configuration.
    53  func ParseSettings(config string) (*notifyConfig.Settings, error) {
    54  	settings := notifyConfig.Settings{}
    55  	if err := proto.UnmarshalText(config, &settings); err != nil {
    56  		return nil, err
    57  	}
    58  	return &settings, nil
    59  }