Skip to content

Move dateStamp getter to utils.dart #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkgs/unified_analytics/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.1.1-dev

- Refactoring `dateStamp` utility function to be defined in `utils.dart` instead of having static methods in `Initializer` and `ConfigHandler`

## 1.1.0

- Added a `okToSend` getter so that clients can easily and accurately check the state of the consent mechanism.
Expand Down
7 changes: 1 addition & 6 deletions pkgs/unified_analytics/lib/src/config_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:path/path.dart' as p;

import 'constants.dart';
import 'initializer.dart';
import 'utils.dart';

/// The regex pattern used to parse the disable analytics line
const String telemetryFlagPattern = r'^reporting=([0|1]) *$';
Expand All @@ -32,12 +33,6 @@ class ConfigHandler {
RegExp(telemetryFlagPattern, multiLine: true);
static RegExp toolRegex = RegExp(toolPattern, multiLine: true);

/// Get a string representation of the current date in the following format
/// yyyy-MM-dd (2023-01-09)
static String get dateStamp {
return DateFormat('yyyy-MM-dd').format(clock.now());
}

final FileSystem fs;
final Directory homeDirectory;
final Initializer initializer;
Expand Down
2 changes: 1 addition & 1 deletion pkgs/unified_analytics/lib/src/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const int kLogFileLength = 2500;
const String kLogFileName = 'dart-flutter-telemetry.log';

/// The current version of the package, should be in line with pubspec version.
const String kPackageVersion = '1.1.0';
const String kPackageVersion = '1.1.1-dev';

/// The minimum length for a session
const int kSessionDurationMinutes = 30;
Expand Down
7 changes: 0 additions & 7 deletions pkgs/unified_analytics/lib/src/initializer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'dart:convert';

import 'package:clock/clock.dart';
import 'package:file/file.dart';
import 'package:intl/intl.dart';
import 'package:path/path.dart' as p;

import 'constants.dart';
Expand Down Expand Up @@ -37,12 +36,6 @@ class Initializer {
required this.pddFlag,
});

/// Get a string representation of the current date in the following format
/// yyyy-MM-dd (2023-01-09)
String get dateStamp {
return DateFormat('yyyy-MM-dd').format(clock.now());
}

/// Creates the text file that will contain the client ID
/// which will be used across all related tools for analytics
/// reporting in GA
Expand Down
8 changes: 8 additions & 0 deletions pkgs/unified_analytics/lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@ import 'dart:convert';
import 'dart:io' as io;
import 'dart:math' show Random;

import 'package:clock/clock.dart';
import 'package:file/file.dart';
import 'package:intl/intl.dart';
import 'package:path/path.dart' as p;

import 'enums.dart';
import 'user_property.dart';

/// Get a string representation of the current date in the following format
/// yyyy-MM-dd (2023-01-09)
String get dateStamp {
return DateFormat('yyyy-MM-dd').format(clock.now());
}

/// Format time as 'yyyy-MM-dd HH:mm:ss Z' where Z is the difference between the
/// timezone of t and UTC formatted according to RFC 822.
String formatDateTime(DateTime t) {
Expand Down
2 changes: 1 addition & 1 deletion pkgs/unified_analytics/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: >-
to Google Analytics.
# When updating this, keep the version consistent with the changelog and the
# value in lib/src/constants.dart.
version: 1.1.0
version: 1.1.1-dev
repository: https://github.com/dart-lang/tools/tree/main/pkgs/unified_analytics

environment:
Expand Down
1 change: 1 addition & 0 deletions pkgs/unified_analytics/test/no_op_analytics_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// BSD-style license that can be found in the LICENSE file.

import 'package:test/test.dart';

import 'package:unified_analytics/unified_analytics.dart';

void main() {
Expand Down
18 changes: 9 additions & 9 deletions pkgs/unified_analytics/test/unified_analytics_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import 'dart:io' as io;
import 'dart:math';

import 'package:clock/clock.dart';
import 'package:unified_analytics/unified_analytics.dart';
import 'package:unified_analytics/src/config_handler.dart';
import 'package:unified_analytics/src/constants.dart';
import 'package:unified_analytics/src/session.dart';
import 'package:unified_analytics/src/user_property.dart';
import 'package:unified_analytics/src/utils.dart';
import 'package:file/file.dart';
import 'package:file/memory.dart';
import 'package:test/test.dart';
import 'package:yaml/yaml.dart';

import 'package:unified_analytics/src/constants.dart';
import 'package:unified_analytics/src/session.dart';
import 'package:unified_analytics/src/user_property.dart';
import 'package:unified_analytics/src/utils.dart';
import 'package:unified_analytics/unified_analytics.dart';

void main() {
late FileSystem fs;
late Directory home;
Expand Down Expand Up @@ -468,8 +468,8 @@ reporting=1
# and the value is a date in the form YYYY-MM-DD, a comma, and
# a number representing the version of the message that was
# displayed.
${initialTool.label}=${ConfigHandler.dateStamp},$toolsMessageVersion
${initialTool.label}=${ConfigHandler.dateStamp},$toolsMessageVersion
${initialTool.label}=$dateStamp,$toolsMessageVersion
${initialTool.label}=$dateStamp,$toolsMessageVersion
''');

// Initialize a second analytics class for the same tool as
Expand Down Expand Up @@ -520,7 +520,7 @@ ${initialTool.label}=${ConfigHandler.dateStamp},$toolsMessageVersion

expect(
configFile.readAsStringSync().endsWith(
'# displayed.\n${initialTool.label}=${ConfigHandler.dateStamp},${toolsMessageVersion + 1}\n'),
'# displayed.\n${initialTool.label}=$dateStamp,${toolsMessageVersion + 1}\n'),
true,
reason: 'The config file ends with the correctly formatted ending '
'after removing the duplicate lines for a given tool',
Expand Down
17 changes: 10 additions & 7 deletions pkgs/unified_analytics/test/workflow_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import 'package:file/file.dart';
import 'package:file/memory.dart';
import 'package:test/test.dart';

import 'package:unified_analytics/src/config_handler.dart';
import 'package:unified_analytics/src/constants.dart';
import 'package:unified_analytics/src/utils.dart';
import 'package:unified_analytics/unified_analytics.dart';

void main() {
Expand Down Expand Up @@ -277,8 +277,9 @@ void main() {
secondAnalytics.clientShowedMessage();

expect(
configFile.readAsStringSync().endsWith(
'${secondTool.label}=${ConfigHandler.dateStamp},$firstVersion\n'),
configFile
.readAsStringSync()
.endsWith('${secondTool.label}=$dateStamp,$firstVersion\n'),
true);

// Create a new instane of the secondTool with an even
Expand All @@ -299,17 +300,19 @@ void main() {
);

expect(
configFile.readAsStringSync().endsWith(
'${secondTool.label}=${ConfigHandler.dateStamp},$firstVersion\n'),
configFile
.readAsStringSync()
.endsWith('${secondTool.label}=$dateStamp,$firstVersion\n'),
true);

// After invoking this method, it will get updated
// in the config with the next version
thirdAnalytics.clientShowedMessage();

expect(
configFile.readAsStringSync().endsWith(
'${secondTool.label}=${ConfigHandler.dateStamp},$secondVersion\n'),
configFile
.readAsStringSync()
.endsWith('${secondTool.label}=$dateStamp,$secondVersion\n'),
true);
});
}