From 03162044c792201a82514607f40b290161683608 Mon Sep 17 00:00:00 2001 From: Rob Becker Date: Sun, 24 Feb 2019 15:45:42 -0700 Subject: [PATCH 1/6] Preserve pedantic include in analysis_options.yaml --- lib/src/update.dart | 54 ++++++++++++------- lib/src/util.dart | 13 +++-- .../includes_pedantic/analysis_options.yaml | 1 + test/vm/check_test.dart | 2 +- test/vm/update_test.dart | 37 +++++++++++++ 5 files changed, 85 insertions(+), 22 deletions(-) create mode 100644 test/fixtures/analysis/includes_pedantic/analysis_options.yaml create mode 100644 test/vm/update_test.dart diff --git a/lib/src/update.dart b/lib/src/update.dart index 345f9b6..a438803 100644 --- a/lib/src/update.dart +++ b/lib/src/update.dart @@ -19,41 +19,53 @@ import 'package:yaml/yaml.dart'; import 'package:abide/src/constants.dart'; import 'package:abide/src/util.dart'; -Future updateAnalysisOption(YamlMap abideYaml, - {bool uncommentClean}) async { - final YamlMap currentAnalysisOptions = - loadAnalysisOptions(renameDeprecatedFilename: true); - final String currentAnalysisOptionsString = loadAnalysisOptionsAsString(); +Future updateAnalysisOption(YamlMap abideYaml, + {String pathToAnalysisOptionsFile, + bool uncommentClean, + bool writeToFile = true}) async { + YamlMap currentAnalysisOptions = loadAnalysisOptions( + pathToAnalysisOptionsFile: pathToAnalysisOptionsFile, + renameDeprecatedFilename: true); + + final String currentAnalysisOptionsString = loadAnalysisOptionsAsString( + pathToAnalysisOptionsFile: pathToAnalysisOptionsFile); + Map> lintErrorCounts = >{}; // Write a version with ALL lint rules enabled // and then run dart analyzer to get counts of lint per rule // those counts are passed into the 2nd write run to decide // whether or not to comment out a lint rule with a lot of - // new lint warnings - writeAnalyisOptionsFile( - all: true, - abideYaml: abideYaml, - currentAnalysisOptions: currentAnalysisOptions, - currentAnalysisOptionsString: currentAnalysisOptionsString); - lintErrorCounts = await getLintErrorCounts(abideYaml: abideYaml); - writeAnalyisOptionsFile( + // new lint warnings. If we're not writing to a file, then lint + // counts won't work, so skip that step. + if (writeToFile) { + writeAnalyisOptionsFile( + all: true, + abideYaml: abideYaml, + currentAnalysisOptions: currentAnalysisOptions, + currentAnalysisOptionsString: currentAnalysisOptionsString); + lintErrorCounts = await getLintErrorCounts(abideYaml: abideYaml); + } + return writeAnalyisOptionsFile( abideYaml: abideYaml, currentAnalysisOptions: currentAnalysisOptions, currentAnalysisOptionsString: currentAnalysisOptionsString, lintErrorCounts: lintErrorCounts, - uncommentClean: uncommentClean); + uncommentClean: uncommentClean, + writeToFile: writeToFile); } -void writeAnalyisOptionsFile( +String writeAnalyisOptionsFile( {YamlMap abideYaml, YamlMap currentAnalysisOptions, String currentAnalysisOptionsString, bool all = false, bool uncommentClean = false, - Map> lintErrorCounts: + bool writeToFile = true, + Map> lintErrorCounts = const >{}}) { currentAnalysisOptions ??= new YamlMap(); + final String currentInclude = getYamlValue(currentAnalysisOptions, 'include'); final bool currentImplicitCasts = getYamlValue( currentAnalysisOptions, 'analyzer:strong-mode:implicit-casts', true); final bool currentImplicitDynamic = getYamlValue( @@ -61,6 +73,11 @@ void writeAnalyisOptionsFile( final StringBuffer sb = new StringBuffer(''' # analysis_options.yaml docs: https://www.dartlang.org/guides/language/analysis-options +'''); + if (currentInclude != null && currentInclude.isNotEmpty) { + sb.writeln('include: $currentInclude'); + } + sb.write(''' analyzer: # Strong mode is required. Applies to the current project. strong-mode: @@ -198,8 +215,8 @@ linter: $output '''); } - - new File(analysisOptionsFilename).writeAsStringSync(sb.toString()); + String finalOutput = sb.toString(); + new File(analysisOptionsFilename).writeAsStringSync(finalOutput); if (!all) { print('Wrote $analysisOptionsFilename'); } @@ -207,6 +224,7 @@ $output print( 'There were missing recommendations. Please inform the maintainers of the abide tool to perform an abide upgrade.'); } + return finalOutput; } String _lintResultFor(String lint, Map> lintErrors) { diff --git a/lib/src/util.dart b/lib/src/util.dart index 44ff44d..9f84a1c 100644 --- a/lib/src/util.dart +++ b/lib/src/util.dart @@ -62,12 +62,19 @@ YamlMap loadPubspec() => loadYamlFile(pubspecFilename); YamlMap loadSmithy() => loadYamlFile(smithyFilename) ?? loadYamlFile(smithyFilename2); -YamlMap loadAnalysisOptions({bool renameDeprecatedFilename: false}) => +YamlMap loadAnalysisOptions( + {String pathToAnalysisOptionsFile, bool renameDeprecatedFilename: false}) { + if (pathToAnalysisOptionsFile != null) { + return loadYamlFile(pathToAnalysisOptionsFile); + } else { loadYamlFile(findAnalysisOptionsFile( renameDeprecatedFilename: renameDeprecatedFilename)); + } +} -String loadAnalysisOptionsAsString() { - final String filename = findAnalysisOptionsFile(); +String loadAnalysisOptionsAsString({String pathToAnalysisOptionsFile}) { + final String filename = + pathToAnalysisOptionsFile ?? findAnalysisOptionsFile(); if (filename == null) { return ''; } diff --git a/test/fixtures/analysis/includes_pedantic/analysis_options.yaml b/test/fixtures/analysis/includes_pedantic/analysis_options.yaml new file mode 100644 index 0000000..108d105 --- /dev/null +++ b/test/fixtures/analysis/includes_pedantic/analysis_options.yaml @@ -0,0 +1 @@ +include: package:pedantic/analysis_options.yaml diff --git a/test/vm/check_test.dart b/test/vm/check_test.dart index fcda08a..b0f225f 100644 --- a/test/vm/check_test.dart +++ b/test/vm/check_test.dart @@ -43,7 +43,7 @@ Future main() async { } }); - group('Verifiy pubspec check', () { + group('Verify pubspec check', () { final YamlMap pubspec = loadPubspec(); final YamlMap pubspecWithMissingDeps = loadYamlFile('test/fixtures/pubspec_missing_deps.yaml'); diff --git a/test/vm/update_test.dart b/test/vm/update_test.dart new file mode 100644 index 0000000..50db780 --- /dev/null +++ b/test/vm/update_test.dart @@ -0,0 +1,37 @@ +// Copyright 2017-2019 Workiva Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +@TestOn('vm') +import 'package:test/test.dart'; +import 'package:yaml/yaml.dart'; + +import 'package:abide/src/util.dart'; +import 'package:abide/src/update.dart'; + +void main() async { + YamlMap abideYaml = await loadAbideYaml(); + + group('Update', () { + test('preserves an existing include key', () async { + String result = await updateAnalysisOption(abideYaml, + pathToAnalysisOptionsFile: + 'test/fixtures/analysis/includes_pedantic/analysis_options.yaml', + uncommentClean: false, + writeToFile: false); + YamlMap yamlOutput = loadYaml(result); + expect(yamlOutput['include'] == 'package:pedantic/analysis_options.yaml', + isTrue); + }); + }); +} From 16b50661987434279dd989623d5e2d92d78e8898 Mon Sep 17 00:00:00 2001 From: Rob Becker Date: Sun, 24 Feb 2019 15:52:20 -0700 Subject: [PATCH 2/6] fix main return type for dart 1 --- test/vm/update_test.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/vm/update_test.dart b/test/vm/update_test.dart index 50db780..be95ddf 100644 --- a/test/vm/update_test.dart +++ b/test/vm/update_test.dart @@ -19,7 +19,7 @@ import 'package:yaml/yaml.dart'; import 'package:abide/src/util.dart'; import 'package:abide/src/update.dart'; -void main() async { +Future main() async { YamlMap abideYaml = await loadAbideYaml(); group('Update', () { From f92da5800a24224e414efa55a3cb6e4fa465b95c Mon Sep 17 00:00:00 2001 From: Rob Becker Date: Sun, 24 Feb 2019 15:53:47 -0700 Subject: [PATCH 3/6] more fixes --- lib/src/util.dart | 2 +- test/vm/update_test.dart | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/src/util.dart b/lib/src/util.dart index 9f84a1c..92e495d 100644 --- a/lib/src/util.dart +++ b/lib/src/util.dart @@ -67,7 +67,7 @@ YamlMap loadAnalysisOptions( if (pathToAnalysisOptionsFile != null) { return loadYamlFile(pathToAnalysisOptionsFile); } else { - loadYamlFile(findAnalysisOptionsFile( + return loadYamlFile(findAnalysisOptionsFile( renameDeprecatedFilename: renameDeprecatedFilename)); } } diff --git a/test/vm/update_test.dart b/test/vm/update_test.dart index be95ddf..89c90da 100644 --- a/test/vm/update_test.dart +++ b/test/vm/update_test.dart @@ -13,6 +13,7 @@ // limitations under the License. @TestOn('vm') +import 'dart:async'; import 'package:test/test.dart'; import 'package:yaml/yaml.dart'; From 6195ffc9e85c00bfa2c61cfaa2c4297694ae50b2 Mon Sep 17 00:00:00 2001 From: Rob Becker Date: Sun, 24 Feb 2019 20:25:53 -0700 Subject: [PATCH 4/6] use newer Dart in CI, only write file if asked --- Dockerfile | 2 +- lib/src/update.dart | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 4d08879..0ab7362 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM google/dart:2.0.0 as build +FROM google/dart:2.1.1 as build # Build Environment Vars ARG BUILD_ID diff --git a/lib/src/update.dart b/lib/src/update.dart index a438803..7e17263 100644 --- a/lib/src/update.dart +++ b/lib/src/update.dart @@ -216,7 +216,9 @@ $output '''); } String finalOutput = sb.toString(); - new File(analysisOptionsFilename).writeAsStringSync(finalOutput); + if (writeToFile) { + new File(analysisOptionsFilename).writeAsStringSync(finalOutput); + } if (!all) { print('Wrote $analysisOptionsFilename'); } From 8f563b20e8bfd01ac5ca31848941d1dac24f4263 Mon Sep 17 00:00:00 2001 From: Rob Becker Date: Tue, 26 Feb 2019 17:06:07 -0700 Subject: [PATCH 5/6] fix bug that showed up in tests : null bool --- lib/src/update.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/update.dart b/lib/src/update.dart index 29522d5..2b827fd 100644 --- a/lib/src/update.dart +++ b/lib/src/update.dart @@ -140,7 +140,7 @@ analyzer: final bool wasPresentCommented = commentedOutLintRule.hasMatch(currentAnalysisOptionsString); final bool wasPresent = - getYamlValue(currentAnalysisOptions, 'linter:rules:$lint'); + getYamlValue(currentAnalysisOptions, 'linter:rules:$lint') ?? false; String issues = _lintResultFor(lint, lintErrorCounts); final bool hasIssues = issues.isNotEmpty; From b3dc4902833719ee488fac4ce78ea7968345b3f4 Mon Sep 17 00:00:00 2001 From: Rob Becker Date: Wed, 27 Feb 2019 12:14:12 -0700 Subject: [PATCH 6/6] refactor based on CR --- lib/src/update.dart | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/lib/src/update.dart b/lib/src/update.dart index 2b827fd..841a273 100644 --- a/lib/src/update.dart +++ b/lib/src/update.dart @@ -38,29 +38,32 @@ Future updateAnalysisOption(YamlMap abideYaml, // new lint warnings. If we're not writing to a file, then lint // counts won't work, so skip that step. if (writeToFile) { - writeAnalyisOptionsFile( + String allRules = generateAnalyisOptionsContent( all: true, abideYaml: abideYaml, currentAnalysisOptions: currentAnalysisOptions, currentAnalysisOptionsString: currentAnalysisOptionsString); + writeAnalysisOptionsFile(allRules); lintErrorCounts = await getLintErrorCounts(abideYaml: abideYaml); } - return writeAnalyisOptionsFile( + String newContent = generateAnalyisOptionsContent( abideYaml: abideYaml, currentAnalysisOptions: currentAnalysisOptions, currentAnalysisOptionsString: currentAnalysisOptionsString, lintErrorCounts: lintErrorCounts, - uncommentClean: uncommentClean, - writeToFile: writeToFile); + uncommentClean: uncommentClean); + if (writeToFile) { + writeAnalysisOptionsFile(newContent); + } + return newContent; } -String writeAnalyisOptionsFile( +String generateAnalyisOptionsContent( {YamlMap abideYaml, YamlMap currentAnalysisOptions, String currentAnalysisOptionsString, bool all = false, bool uncommentClean = false, - bool writeToFile = true, Map> lintErrorCounts = const >{}}) { currentAnalysisOptions ??= new YamlMap(); @@ -219,18 +222,16 @@ linter: $output '''); } - String finalOutput = sb.toString(); - if (writeToFile) { - new File(analysisOptionsFilename).writeAsStringSync(finalOutput); - } - if (!all) { - print('Wrote $analysisOptionsFilename'); - } + if (nMissingRecommendations > 0) { print( 'There were missing recommendations. Please inform the maintainers of the abide tool to perform an abide upgrade.'); } - return finalOutput; + return sb.toString(); +} + +void writeAnalysisOptionsFile(String content) { + new File(analysisOptionsFilename).writeAsStringSync(content); } String _lintResultFor(String lint, Map> lintErrors) {