1// Copyright 2018 The Hugo Authors. All rights reserved.
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// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14package hugofs
15
16import (
17	"testing"
18
19	qt "github.com/frankban/quicktest"
20	"github.com/spf13/afero"
21)
22
23type testHashReceiver struct {
24	sum  string
25	name string
26}
27
28func (t *testHashReceiver) OnFileClose(name, md5hash string) {
29	t.name = name
30	t.sum = md5hash
31}
32
33func TestHashingFs(t *testing.T) {
34	c := qt.New(t)
35
36	fs := afero.NewMemMapFs()
37	observer := &testHashReceiver{}
38	ofs := NewHashingFs(fs, observer)
39
40	f, err := ofs.Create("hashme")
41	c.Assert(err, qt.IsNil)
42	_, err = f.Write([]byte("content"))
43	c.Assert(err, qt.IsNil)
44	c.Assert(f.Close(), qt.IsNil)
45	c.Assert(observer.sum, qt.Equals, "9a0364b9e99bb480dd25e1f0284c8555")
46	c.Assert(observer.name, qt.Equals, "hashme")
47
48	f, err = ofs.Create("nowrites")
49	c.Assert(err, qt.IsNil)
50	c.Assert(f.Close(), qt.IsNil)
51	c.Assert(observer.sum, qt.Equals, "d41d8cd98f00b204e9800998ecf8427e")
52}
53