Skip to content

Commit

Permalink
Truncating host os if necessary + tests for util function
Browse files Browse the repository at this point in the history
  • Loading branch information
eliasyishak committed Nov 7, 2023
1 parent 5f8f51e commit 6ee9487
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pkgs/unified_analytics/lib/src/analytics.dart
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,10 @@ class AnalyticsImpl implements Analytics {
flutterVersion: flutterVersion,
dartVersion: dartVersion,
tool: tool.label,
hostOsVersion: io.Platform.operatingSystemVersion,
// We truncate this to a maximum of 36 characters since this can
// a very long string for some operating systems
hostOsVersion:
truncateStringToLength(io.Platform.operatingSystemVersion, 36),
locale: io.Platform.localeName,
clientIde: clientIde,
);
Expand Down
29 changes: 29 additions & 0 deletions pkgs/unified_analytics/lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,35 @@ bool surveySnoozedOrDismissed(
return survey.snoozeForMinutes > minutesElapsed;
}

/// Due to some limitations for GA4, this function can be used to
/// truncate fields that we may not care about truncating, such as
/// the host os details.
///
/// [maxLength] represents the maximum length allowed for the string.
///
/// Example:
/// "Linux 6.2.0-1015-azure #15~22.04.1-Ubuntu SMP Fri Oct 6 13:20:44 UTC 2023"
///
/// The above string is what is returned by [io.Platform.operatingSystemVersion]
/// for certain machines running GitHub Actions, this function will truncate that
/// value down to the maximum length at 36 characters and return the below
///
/// Return:
/// "Linux 6.2.0-1015-azure #15~22."
///
/// This should only be used on fields that are okay to be truncated, this
/// should not be used for parameters on the [Event] constructors.
String truncateStringToLength(String str, int maxLength) {
if (maxLength <= 0) {
throw ArgumentError(
'The length to truncate a string must be greater than 0');
}

if (maxLength > str.length) return str;

return str.substring(0, maxLength);
}

/// A UUID generator.
///
/// This will generate unique IDs in the format:
Expand Down
57 changes: 57 additions & 0 deletions pkgs/unified_analytics/test/log_handler_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:test/test.dart';

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

void main() {
Expand Down Expand Up @@ -185,4 +186,60 @@ void main() {
expect(secondLogFileStats, isNotNull);
expect(secondLogFileStats!.recordCount, countOfEventsToSend);
});

test(
'truncateStringToLength returns same string when '
'max length greater than string length', () {
final testString = 'Version 14.1 (Build 23B74)';
final maxLength = 100;

expect(testString.length < maxLength, true);

String runTruncateString() => truncateStringToLength(testString, maxLength);

expect(runTruncateString, returnsNormally);

final newString = runTruncateString();
expect(newString, testString);
});

test(
'truncateStringToLength returns truncated string when '
'max length less than string length', () {
final testString = 'Version 14.1 (Build 23B74)';
final maxLength = 10;

expect(testString.length > maxLength, true);

String runTruncateString() => truncateStringToLength(testString, maxLength);

expect(runTruncateString, returnsNormally);

final newString = runTruncateString();
expect(newString.length, maxLength);
expect(newString, 'Version 14');
});

test('truncateStringToLength handle errors for invalid max length', () {
final testString = 'Version 14.1 (Build 23B74)';
var maxLength = 0;
String runTruncateString() => truncateStringToLength(testString, maxLength);

expect(runTruncateString, throwsArgumentError);

maxLength = -1;
expect(runTruncateString, throwsArgumentError);
});

test('truncateStringToLength same string when max length is the same', () {
final testString = 'Version 14.1 (Build 23B74)';
final maxLength = testString.length;

String runTruncateString() => truncateStringToLength(testString, maxLength);
expect(runTruncateString, returnsNormally);

final newString = runTruncateString();
expect(newString.length, maxLength);
expect(newString, testString);
});
}

0 comments on commit 6ee9487

Please sign in to comment.