danger-compose_compiler_metrics 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,45 +2,469 @@
2
2
 
3
3
  require File.expand_path("spec_helper", __dir__)
4
4
 
5
- module Danger
6
- describe Danger::DangerComposeCompilerMetrics do
7
- it "should be a plugin" do
8
- expect(Danger::DangerComposeCompilerMetrics.new(nil)).to be_a Danger::Plugin
5
+ describe Danger::DangerComposeCompilerMetrics do
6
+ let(:dangerfile) { testing_dangerfile }
7
+ let(:plugin) { dangerfile.compose_compiler_metrics }
8
+
9
+ describe "#new" do
10
+ subject { plugin }
11
+
12
+ it { is_expected.to be_a Danger::Plugin }
13
+ end
14
+
15
+ describe "#report_difference" do
16
+ subject { plugin.report_difference(metrics_path, reference_metrics_path, options) }
17
+
18
+ let(:file_timestamp) { Time.new(2024, 1, 1, 0, 0, 0) }
19
+ let(:metrics_path) { "#{File.dirname(__FILE__)}/support/fixtures/compose_compiler_metrics" }
20
+ let(:reference_metrics_path) { "#{File.dirname(__FILE__)}/support/fixtures/compose_compiler_metrics_baseline" }
21
+ let(:options) { {} }
22
+
23
+ before do
24
+ [
25
+ metrics_path,
26
+ reference_metrics_path
27
+ ].each do |dir|
28
+ Dir.glob("#{dir}/*").each do |file|
29
+ FileUtils.touch(file, mtime: file_timestamp)
30
+ end
31
+ end
32
+ end
33
+
34
+ let(:expect_report_list) do
35
+ [
36
+ "# Compose Compiler Metrics Difference Report",
37
+ "## app - debug",
38
+ <<~MARKDOWN,
39
+ <details >
40
+ <summary>
41
+
42
+ ### Metrics Summary
43
+
44
+ </summary>
45
+
46
+ #### Composables
47
+
48
+ | name | reference | new | diff |
49
+ | --- | --- | --- | --- |
50
+ | skippableComposables | 5 | 6 | +1 |
51
+ | unskippableComposables | 3 | 1 | -2 |
52
+ | restartableComposables | 8 | 7 | -1 |
53
+ | unrestartableComposables | 0 | 0 | |
54
+ | readonlyComposables | 0 | 0 | |
55
+ | totalComposables | 8 | 7 | -1 |
56
+
57
+ #### Groups
58
+
59
+ | name | reference | new | diff |
60
+ | --- | --- | --- | --- |
61
+ | restartGroups | 8 | 7 | -1 |
62
+ | totalGroups | 11 | 7 | -4 |
63
+
64
+ #### Arguments
65
+
66
+ | name | reference | new | diff |
67
+ | --- | --- | --- | --- |
68
+ | staticArguments | 5 | 4 | -1 |
69
+ | certainArguments | 5 | 4 | -1 |
70
+ | knownStableArguments | 50 | 47 | -3 |
71
+ | knownUnstableArguments | 2 | 0 | -2 |
72
+ | unknownStableArguments | 0 | 0 | |
73
+ | totalArguments | 52 | 47 | -5 |
74
+
75
+ #### Classes
76
+
77
+ | name | reference | new | diff |
78
+ | --- | --- | --- | --- |
79
+ | markedStableClasses | 0 | 0 | |
80
+ | inferredStableClasses | 1 | 2 | +1 |
81
+ | inferredUnstableClasses | 1 | 0 | -1 |
82
+ | inferredUncertainClasses | 0 | 0 | |
83
+ | effectivelyStableClasses | 1 | 2 | +1 |
84
+ | totalClasses | 2 | 2 | |
85
+
86
+ #### Lambdas
87
+
88
+ | name | reference | new | diff |
89
+ | --- | --- | --- | --- |
90
+ | memoizedLambdas | 4 | 4 | |
91
+ | singletonLambdas | 0 | 0 | |
92
+ | singletonComposableLambdas | 3 | 3 | |
93
+ | composableLambdas | 3 | 3 | |
94
+ | totalLambdas | 5 | 4 | -1 |
95
+
96
+ </details>
97
+ MARKDOWN
98
+ <<~MARKDOWN,
99
+ <details >
100
+ <summary>
101
+
102
+ ### Metrics
103
+
104
+ </summary>
105
+
106
+ ```diff
107
+ --- #{File.dirname(__FILE__)}/support/fixtures/compose_compiler_metrics_baseline/app_debug-module.json #{file_timestamp.strftime('%F %T.%N %z')}
108
+ +++ #{File.dirname(__FILE__)}/support/fixtures/compose_compiler_metrics/app_debug-module.json #{file_timestamp.strftime('%F %T.%N %z')}
109
+ @@ -1,25 +1,25 @@
110
+ {
111
+ - "skippableComposables": 5,
112
+ - "restartableComposables": 8,
113
+ + "skippableComposables": 6,
114
+ + "restartableComposables": 7,
115
+ "readonlyComposables": 0,
116
+ - "totalComposables": 8,
117
+ - "restartGroups": 8,
118
+ - "totalGroups": 11,
119
+ - "staticArguments": 5,
120
+ - "certainArguments": 5,
121
+ - "knownStableArguments": 50,
122
+ - "knownUnstableArguments": 2,
123
+ + "totalComposables": 7,
124
+ + "restartGroups": 7,
125
+ + "totalGroups": 7,
126
+ + "staticArguments": 4,
127
+ + "certainArguments": 4,
128
+ + "knownStableArguments": 47,
129
+ + "knownUnstableArguments": 0,
130
+ "unknownStableArguments": 0,
131
+ - "totalArguments": 52,
132
+ + "totalArguments": 47,
133
+ "markedStableClasses": 0,
134
+ - "inferredStableClasses": 1,
135
+ - "inferredUnstableClasses": 1,
136
+ + "inferredStableClasses": 2,
137
+ + "inferredUnstableClasses": 0,
138
+ "inferredUncertainClasses": 0,
139
+ - "effectivelyStableClasses": 1,
140
+ + "effectivelyStableClasses": 2,
141
+ "totalClasses": 2,
142
+ "memoizedLambdas": 4,
143
+ "singletonLambdas": 0,
144
+ "singletonComposableLambdas": 3,
145
+ "composableLambdas": 3,
146
+ - "totalLambdas": 5
147
+ + "totalLambdas": 4
148
+ }
149
+ \
150
+
151
+ ```
152
+
153
+
154
+ </details>
155
+ MARKDOWN
156
+ <<~MARKDOWN,
157
+ <details >
158
+ <summary>
159
+
160
+ ### Composable Stats Report
161
+
162
+ </summary>
163
+
164
+ ```diff
165
+ --- #{File.dirname(__FILE__)}/support/fixtures/compose_compiler_metrics_baseline/app_debug-composables.csv #{file_timestamp.strftime('%F %T.%N %z')}
166
+ +++ #{File.dirname(__FILE__)}/support/fixtures/compose_compiler_metrics/app_debug-composables.csv #{file_timestamp.strftime('%F %T.%N %z')}
167
+ @@ -1,5 +1,4 @@
168
+ package,name,composable,skippable,restartable,readonly,inline,isLambda,hasDefaults,defaultsGroup,groups,calls,
169
+ -com.example.android.ContactRow,ContactRow,1,0,1,0,0,0,0,0,1,2,
170
+ +com.example.android.ContactRow,ContactRow,1,1,1,0,0,0,0,0,1,2,
171
+ com.example.android.ToggleButton,ToggleButton,1,1,1,0,0,0,0,0,1,1,
172
+ -com.example.android.ContactDetails,ContactDetails,1,0,1,0,0,0,0,0,1,1,
173
+ -com.example.android.ui.theme.ExampleTheme,ExampleTheme,1,1,1,0,0,0,1,0,4,5,
174
+ +com.example.android.ContactDetails,ContactDetails,1,1,1,0,0,0,0,0,1,1,
175
+
176
+ ```
177
+
178
+
179
+ </details>
180
+ MARKDOWN
181
+ <<~MARKDOWN,
182
+ <details >
183
+ <summary>
184
+
185
+ ### Composable Report
186
+
187
+ </summary>
188
+
189
+ ```diff
190
+ --- #{File.dirname(__FILE__)}/support/fixtures/compose_compiler_metrics_baseline/app_debug-composables.txt #{file_timestamp.strftime('%F %T.%N %z')}
191
+ +++ #{File.dirname(__FILE__)}/support/fixtures/compose_compiler_metrics/app_debug-composables.txt #{file_timestamp.strftime('%F %T.%N %z')}
192
+ @@ -1,16 +1,11 @@
193
+ -restartable scheme("[androidx.compose.ui.UiComposable]") fun ContactRow(
194
+ - unstable contact: Contact
195
+ +restartable skippable scheme("[androidx.compose.ui.UiComposable]") fun ContactRow(
196
+ + stable contact: Contact
197
+ stable modifier: Modifier? = @static Companion
198
+ )
199
+ restartable skippable scheme("[androidx.compose.ui.UiComposable]") fun ToggleButton(
200
+ stable selected: Boolean
201
+ stable onToggled: Function1<Boolean, Unit>
202
+ )
203
+ -restartable scheme("[androidx.compose.ui.UiComposable]") fun ContactDetails(
204
+ - unstable contact: Contact
205
+ -)
206
+ -restartable skippable scheme("[0, [0]]") fun ExampleTheme(
207
+ - stable darkTheme: Boolean = @dynamic isSystemInDarkTheme($composer, 0)
208
+ - stable dynamicColor: Boolean = @static true
209
+ - stable content: Function2<Composer, Int, Unit>
210
+ +restartable skippable scheme("[androidx.compose.ui.UiComposable]") fun ContactDetails(
211
+ + stable contact: Contact
212
+ )
213
+
214
+ ```
215
+
216
+
217
+ </details>
218
+ MARKDOWN
219
+ <<~MARKDOWN
220
+ <details >
221
+ <summary>
222
+
223
+ ### Class Report
224
+
225
+ </summary>
226
+
227
+ ```diff
228
+ --- #{File.dirname(__FILE__)}/support/fixtures/compose_compiler_metrics_baseline/app_debug-classes.txt #{file_timestamp.strftime('%F %T.%N %z')}
229
+ +++ #{File.dirname(__FILE__)}/support/fixtures/compose_compiler_metrics/app_debug-classes.txt #{file_timestamp.strftime('%F %T.%N %z')}
230
+ @@ -1,8 +1,8 @@
231
+ stable class MainActivity {
232
+ <runtime stability> = Stable
233
+ }
234
+ -unstable class Contact {
235
+ - stable var name: String
236
+ +stable class Contact {
237
+ + stable val name: String
238
+ stable val number: String
239
+ - <runtime stability> = Unstable
240
+ + <runtime stability> = Stable
241
+ }
242
+
243
+ ```
244
+
245
+
246
+ </details>
247
+ MARKDOWN
248
+ ]
249
+ end
250
+
251
+ it "output markdown summary" do
252
+ subject
253
+
254
+ dangerfile.status_report[:markdowns].map(&:message).each_with_index do |message, index|
255
+ expect(message).to eq(expect_report_list[index])
256
+ end
9
257
  end
10
258
 
11
- #
12
- # You should test your custom attributes and methods here
13
- #
14
- describe "with Dangerfile" do
259
+ context "when missing diff command" do
15
260
  before do
16
- @dangerfile = testing_dangerfile
17
- @my_plugin = @dangerfile.compose_compiler_metrics
261
+ allow_any_instance_of(Helper).to receive(:installed?).with("diff").and_return(false)
262
+ end
18
263
 
19
- # mock the PR data
20
- # you can then use this, eg. github.pr_author, later in the spec
21
- json = File.read("#{File.dirname(__FILE__)}/support/fixtures/github_pr.json") # example json: `curl https://api.github.com/repos/danger/danger-plugin-template/pulls/18 > github_pr.json`
22
- allow(@my_plugin.github).to receive(:pr_json).and_return(json)
264
+ it do
265
+ within_block_is_expected.to change {
266
+ dangerfile.status_report[:errors]
267
+ }.from(
268
+ be_empty
269
+ ).to(
270
+ ["diff command not found. Please install diff command."]
271
+ )
23
272
  end
273
+ end
24
274
 
25
- # Some examples for writing tests
26
- # You should replace these with your own.
275
+ context "when missing reference metrics file" do
276
+ let(:reference_metrics_path) { "#{File.dirname(__FILE__)}/support/fixtures/compose_compiler_metrics_missing" }
27
277
 
28
- xit "Warns on a monday" do
29
- monday_date = Date.parse("2016-07-11")
30
- allow(Date).to receive(:today).and_return monday_date
278
+ it do
279
+ within_block_is_expected.to change {
280
+ dangerfile.status_report[:warnings]
281
+ }.from(
282
+ be_empty
283
+ ).to(
284
+ [
285
+ "DangerComposeCompilerMetrics: reference file not found at #{File.dirname(__FILE__)}/support/fixtures/compose_compiler_metrics_missing/app_debug-module.json. Skipping file difference report.",
286
+ "DangerComposeCompilerMetrics: reference file not found at #{File.dirname(__FILE__)}/support/fixtures/compose_compiler_metrics_missing/app_debug-module.json. Skipping file difference report.",
287
+ "DangerComposeCompilerMetrics: reference file not found at #{File.dirname(__FILE__)}/support/fixtures/compose_compiler_metrics_missing/app_debug-composables.csv. Skipping file difference report.",
288
+ "DangerComposeCompilerMetrics: reference file not found at #{File.dirname(__FILE__)}/support/fixtures/compose_compiler_metrics_missing/app_debug-composables.txt. Skipping file difference report.",
289
+ "DangerComposeCompilerMetrics: reference file not found at #{File.dirname(__FILE__)}/support/fixtures/compose_compiler_metrics_missing/app_debug-classes.txt. Skipping file difference report."
290
+ ]
291
+ )
292
+ end
293
+ end
31
294
 
32
- @my_plugin.warn_on_mondays
295
+ context "when metrics option is disabled" do
296
+ let(:options) { { metrics: :disabled } }
33
297
 
34
- expect(@dangerfile.status_report[:warnings]).to eq(["Trying to merge code on a Monday"])
298
+ it do
299
+ within_block_is_expected.to change {
300
+ dangerfile.status_report[:markdowns].map(&:message)
301
+ }.from(
302
+ be_empty
303
+ ).to(
304
+ expect_report_list.reject.with_index { |_, i| i == 3 }
305
+ )
35
306
  end
307
+ end
308
+
309
+ context "when metrics option is open" do
310
+ let(:options) { { metrics: :open } }
311
+
312
+ it do
313
+ subject
314
+
315
+ expect(dangerfile.status_report[:markdowns][3].message).to eq(
316
+ expect_report_list[3].gsub("<details >", "<details open>")
317
+ )
318
+ end
319
+ end
320
+ end
321
+
322
+ describe "#report" do
323
+ subject { plugin.report(metrics_path, options) }
324
+
325
+ let(:metrics_path) { "#{File.dirname(__FILE__)}/support/fixtures/compose_compiler_metrics" }
326
+ let(:options) { {} }
327
+ let(:expect_report_list) do
328
+ [
329
+ "# Compose Compiler Metrics Report",
330
+ "## app - debug",
331
+ <<~MARKDOWN,
332
+ <details >
333
+ <summary>
334
+
335
+ ### Metrics
336
+
337
+ </summary>
338
+
339
+ | name | value |
340
+ | --- | --- |
341
+ | skippableComposables | 6 |
342
+ | restartableComposables | 7 |
343
+ | readonlyComposables | 0 |
344
+ | totalComposables | 7 |
345
+ | restartGroups | 7 |
346
+ | totalGroups | 7 |
347
+ | staticArguments | 4 |
348
+ | certainArguments | 4 |
349
+ | knownStableArguments | 47 |
350
+ | knownUnstableArguments | 0 |
351
+ | unknownStableArguments | 0 |
352
+ | totalArguments | 47 |
353
+ | markedStableClasses | 0 |
354
+ | inferredStableClasses | 2 |
355
+ | inferredUnstableClasses | 0 |
356
+ | inferredUncertainClasses | 0 |
357
+ | effectivelyStableClasses | 2 |
358
+ | totalClasses | 2 |
359
+ | memoizedLambdas | 4 |
360
+ | singletonLambdas | 0 |
361
+ | singletonComposableLambdas | 3 |
362
+ | composableLambdas | 3 |
363
+ | totalLambdas | 4 |
364
+
365
+ </details>
366
+ MARKDOWN
367
+ <<~MARKDOWN,
368
+ <details >
369
+ <summary>
370
+
371
+ ### Composable Stats Report
372
+
373
+ </summary>
374
+
375
+ | package | name | composable | skippable | restartable | readonly | inline | isLambda | hasDefaults | defaultsGroup | groups | calls | |
376
+ | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
377
+ | com.example.android.ContactRow | ContactRow | 1 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 1 | 2 | |
378
+ | com.example.android.ToggleButton | ToggleButton | 1 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | |
379
+ | com.example.android.ContactDetails | ContactDetails | 1 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | |
380
+
381
+ </details>
382
+ MARKDOWN
383
+ <<~MARKDOWN,
384
+ <details >
385
+ <summary>
386
+
387
+ ### Composable Report
388
+
389
+ </summary>
390
+
391
+ ```kotlin
392
+ restartable skippable scheme("[androidx.compose.ui.UiComposable]") fun ContactRow(
393
+ stable contact: Contact
394
+ stable modifier: Modifier? = @static Companion
395
+ )
396
+ restartable skippable scheme("[androidx.compose.ui.UiComposable]") fun ToggleButton(
397
+ stable selected: Boolean
398
+ stable onToggled: Function1<Boolean, Unit>
399
+ )
400
+ restartable skippable scheme("[androidx.compose.ui.UiComposable]") fun ContactDetails(
401
+ stable contact: Contact
402
+ )
403
+
404
+ ```
405
+
406
+
407
+ </details>
408
+ MARKDOWN
409
+ <<~MARKDOWN
410
+ <details >
411
+ <summary>
412
+
413
+ ### Class Report
414
+
415
+ </summary>
416
+
417
+ ```kotlin
418
+ stable class MainActivity {
419
+ <runtime stability> = Stable
420
+ }
421
+ stable class Contact {
422
+ stable val name: String
423
+ stable val number: String
424
+ <runtime stability> = Stable
425
+ }
426
+
427
+ ```
428
+
429
+
430
+ </details>
431
+ MARKDOWN
432
+ ]
433
+ end
434
+
435
+ it do
436
+ within_block_is_expected.to change {
437
+ dangerfile.status_report[:markdowns].map(&:message)
438
+ }.from(
439
+ be_empty
440
+ ).to(
441
+ expect_report_list
442
+ )
443
+ end
444
+
445
+ context "when metrics option is disabled" do
446
+ let(:options) { { metrics: :disabled } }
447
+
448
+ it do
449
+ within_block_is_expected.to change {
450
+ dangerfile.status_report[:markdowns].map(&:message)
451
+ }.from(
452
+ be_empty
453
+ ).to(
454
+ expect_report_list.reject.with_index { |_, i| i == 2 }
455
+ )
456
+ end
457
+ end
36
458
 
37
- xit "Does nothing on a tuesday" do
38
- monday_date = Date.parse("2016-07-12")
39
- allow(Date).to receive(:today).and_return monday_date
459
+ context "when metrics option is open" do
460
+ let(:options) { { metrics: :open } }
40
461
 
41
- @my_plugin.warn_on_mondays
462
+ it do
463
+ subject
42
464
 
43
- expect(@dangerfile.status_report[:warnings]).to eq([])
465
+ expect(dangerfile.status_report[:markdowns][2].message).to eq(
466
+ expect_report_list[2].gsub("<details >", "<details open>")
467
+ )
44
468
  end
45
469
  end
46
470
  end