local_cache_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright 2021 gorse Project 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. package server
  15. import (
  16. "github.com/stretchr/testify/assert"
  17. "os"
  18. "path/filepath"
  19. "testing"
  20. )
  21. func TestLocalCache(t *testing.T) {
  22. // delete test file if exists
  23. path := filepath.Join(os.TempDir(), "TestLocalCache_Server")
  24. _ = os.Remove(path)
  25. // load non-existed file
  26. cache, err := LoadLocalCache(path)
  27. assert.Error(t, err)
  28. assert.Equal(t, path, cache.path)
  29. assert.Empty(t, cache.ServerName)
  30. // write and load
  31. cache.ServerName = "Server"
  32. assert.NoError(t, cache.WriteLocalCache())
  33. read, err := LoadLocalCache(path)
  34. assert.NoError(t, err)
  35. assert.Equal(t, "Server", read.ServerName)
  36. // delete test file
  37. assert.NoError(t, os.Remove(path))
  38. }