1// Copyright 2020 Google LLC 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// https://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 15package config 16 17import ( 18 "bytes" 19 "reflect" 20 "testing" 21) 22 23const testWriteExpectedOutput = `{ 24 "DefaultCodebase": "test-codebase", 25 "Codebases": { 26 "test-codebase": "/path/to/test/codebase" 27 }, 28 "Workspaces": {} 29}` 30 31func TestWrite(t *testing.T) { 32 inputConfig := Config{ 33 DefaultCodebase: "test-codebase", 34 Codebases: map[string]string{ 35 "test-codebase": "/path/to/test/codebase", 36 }, 37 Workspaces: map[string]string{}} 38 var outputBuffer bytes.Buffer 39 if err := inputConfig.Write(&outputBuffer); err != nil { 40 t.Error(err) 41 } 42 expectedOutput := []byte(testWriteExpectedOutput) 43 if bytes.Compare(outputBuffer.Bytes(), expectedOutput) != 0 { 44 t.Errorf("Output %s is different that expected output %s", 45 string(outputBuffer.Bytes()), string(expectedOutput)) 46 } 47} 48 49const testReadInput = `{ 50 "DefaultCodebase": "test-codebase", 51 "Codebases": { 52 "test-codebase": "/path/to/test/codebase" 53 }, 54 "Workspaces": {} 55}` 56 57func TestRead(t *testing.T) { 58 inputBytes := []byte(testReadInput) 59 inputBuffer := bytes.NewBuffer(inputBytes) 60 var outputConfig Config 61 if err := outputConfig.Read(inputBuffer); err != nil { 62 t.Error(err) 63 } 64 expectedOutput := Config{ 65 DefaultCodebase: "test-codebase", 66 Codebases: map[string]string{ 67 "test-codebase": "/path/to/test/codebase", 68 }, 69 Workspaces: map[string]string{}} 70 if !reflect.DeepEqual(outputConfig, expectedOutput) { 71 t.Errorf("Written config %v is different than read config %v", 72 outputConfig, expectedOutput) 73 } 74} 75