cfg.TestManualConfigFromYaml   F
last analyzed

Complexity

Conditions 14

Size

Total Lines 46
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 29
nop 1
dl 0
loc 46
rs 3.6
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like cfg.TestManualConfigFromYaml often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
package cfg
2
3
import (
4
	"testing"
5
6
	"gopkg.in/yaml.v2"
7
)
8
9
var yml = `
10
package: fake/testing
11
description: foo bar baz
12
homepage: https://example.com
13
license: MIT
14
owners:
15
- name: foo
16
  email: [email protected]
17
  homepage: https://example.com
18
import:
19
  - package: github.com/kylelemons/go-gypsy
20
    subpackages:
21
      - yaml
22
  # Intentionally left spaces at end of next line.
23
  - package: github.com/Masterminds/convert
24
    repo: [email protected]:Masterminds/convert.git
25
    vcs: git
26
    ref: a9949121a2e2192ca92fa6dddfeaaa4a4412d955
27
    subpackages:
28
      - color
29
      - nautical
30
      - radial
31
    os:
32
      - linux
33
    arch:
34
      - i386
35
      - arm
36
  - package: github.com/Masterminds/structable
37
  - package: github.com/Masterminds/cookoo/color
38
  - package: github.com/Masterminds/cookoo/convert
39
40
testImport:
41
  - package: github.com/kylelemons/go-gypsy
42
`
43
44
func TestManualConfigFromYaml(t *testing.T) {
45
	cfg := &Config{}
46
	err := yaml.Unmarshal([]byte(yml), &cfg)
47
	if err != nil {
48
		t.Errorf("Unable to Unmarshal config yaml")
49
	}
50
51
	if cfg.Name != "fake/testing" {
52
		t.Errorf("Inaccurate name found %s", cfg.Name)
53
	}
54
55
	if cfg.Description != "foo bar baz" {
56
		t.Errorf("Inaccurate description found %s", cfg.Description)
57
	}
58
59
	if cfg.Home != "https://example.com" {
60
		t.Errorf("Inaccurate homepage found %s", cfg.Home)
61
	}
62
63
	if cfg.License != "MIT" {
64
		t.Errorf("Inaccurate license found %s", cfg.License)
65
	}
66
67
	found := false
68
	found2 := false
69
	for _, i := range cfg.Imports {
70
		if i.Name == "github.com/Masterminds/convert" {
71
			found = true
72
			ref := "a9949121a2e2192ca92fa6dddfeaaa4a4412d955"
73
			if i.Reference != ref {
74
				t.Errorf("Config reference for cookoo is inaccurate. Expected '%s' found '%s'", ref, i.Reference)
75
			}
76
		}
77
78
		if i.Name == "github.com/Masterminds/cookoo" {
79
			found2 = true
80
			if i.Subpackages[0] != "color" {
81
				t.Error("Dependency separating package and subpackage not working")
82
			}
83
		}
84
	}
85
	if !found {
86
		t.Error("Unable to find github.com/Masterminds/convert")
87
	}
88
	if !found2 {
89
		t.Error("Unable to find github.com/Masterminds/cookoo")
90
	}
91
}
92
93
func TestClone(t *testing.T) {
94
	cfg := &Config{}
95
	err := yaml.Unmarshal([]byte(yml), &cfg)
96
	if err != nil {
97
		t.Errorf("Unable to Unmarshal config yaml")
98
	}
99
100
	cfg2 := cfg.Clone()
101
	if cfg2.Name != "fake/testing" {
102
		t.Error("Config cloning failed")
103
	}
104
	if cfg2.License != "MIT" {
105
		t.Error("Config cloning failed to copy License")
106
	}
107
	cfg.Name = "foo"
108
109
	if cfg.Name == cfg2.Name {
110
		t.Error("Cloning Config name failed")
111
	}
112
}
113
114
func TestConfigFromYaml(t *testing.T) {
115
	c, err := ConfigFromYaml([]byte(yml))
116
	if err != nil {
117
		t.Error("ConfigFromYaml failed to parse yaml")
118
	}
119
120
	if c.Name != "fake/testing" {
121
		t.Error("ConfigFromYaml failed to properly parse yaml")
122
	}
123
}
124
125
func TestHasDependency(t *testing.T) {
126
	c, err := ConfigFromYaml([]byte(yml))
127
	if err != nil {
128
		t.Error("ConfigFromYaml failed to parse yaml for HasDependency")
129
	}
130
131
	if c.HasDependency("github.com/Masterminds/convert") != true {
132
		t.Error("HasDependency failing to pickup depenency")
133
	}
134
135
	if c.HasDependency("foo/bar/bar") != false {
136
		t.Error("HasDependency picking up dependency it shouldn't")
137
	}
138
}
139
140
func TestOwners(t *testing.T) {
141
	o := new(Owner)
142
	o.Name = "foo"
143
	o.Email = "[email protected]"
144
	o.Home = "https://foo.example.com"
145
146
	o2 := o.Clone()
147
	if o2.Name != o.Name || o2.Email != o.Email || o2.Home != o.Home {
148
		t.Error("Unable to clone Owner")
149
	}
150
151
	o.Name = "Bar"
152
	if o.Name == o2.Name {
153
		t.Error("Owner clone is a pointer instead of a clone")
154
	}
155
156
	s := make(Owners, 0, 1)
157
	s = append(s, o)
158
	s2 := s.Clone()
159
	o3 := s2[0]
160
161
	o3.Name = "Qux"
162
163
	if o3.Name == o.Name {
164
		t.Error("Owners cloning isn't deep")
165
	}
166
167
	cfg := &Config{}
168
	err := yaml.Unmarshal([]byte(yml), &cfg)
169
	if err != nil {
170
		t.Errorf("Unable to Unmarshal config yaml")
171
	}
172
173
	if cfg.Owners[0].Name != "foo" ||
174
		cfg.Owners[0].Email != "[email protected]" ||
175
		cfg.Owners[0].Home != "https://example.com" {
176
		t.Error("Unable to parse owners from yaml")
177
	}
178
}
179