Skip to content
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

[sync] 2022/9/20 #385

Merged
merged 16 commits into from
Sep 29, 2022
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
6 changes: 3 additions & 3 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@c7f292ea4f542c473194b33813ccd4c207a6c725
uses: github/codeql-action/init@904260d7d935dff982205cbdb42025ce30b7a34f
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
Expand All @@ -44,7 +44,7 @@ jobs:
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@c7f292ea4f542c473194b33813ccd4c207a6c725
uses: github/codeql-action/autobuild@904260d7d935dff982205cbdb42025ce30b7a34f

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl
Expand All @@ -58,4 +58,4 @@ jobs:
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@c7f292ea4f542c473194b33813ccd4c207a6c725
uses: github/codeql-action/analyze@904260d7d935dff982205cbdb42025ce30b7a34f
5 changes: 3 additions & 2 deletions .github/workflows/scorecards-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
security-events: write
actions: read
contents: read
id-token: write

steps:
- name: "Checkout code"
Expand All @@ -25,7 +26,7 @@ jobs:
persist-credentials: false

- name: "Run analysis"
uses: ossf/scorecard-action@ce330fde6b1a5c9c75b417e7efc510b822a35564
uses: ossf/scorecard-action@865b4092859256271290c77adbd10a43f4779972
with:
results_file: results.sarif
results_format: sarif
Expand All @@ -48,6 +49,6 @@ jobs:

# Upload the results to GitHub's code scanning dashboard.
- name: "Upload to code-scanning"
uses: github/codeql-action/upload-sarif@c7f292ea4f542c473194b33813ccd4c207a6c725
uses: github/codeql-action/upload-sarif@904260d7d935dff982205cbdb42025ce30b7a34f
with:
sarif_file: results.sarif
14 changes: 7 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,22 @@ ENV PATH=$DART_SDK/bin:$PATH
RUN set -eu; \
case "$(dpkg --print-architecture)_${DART_CHANNEL}" in \
amd64_stable) \
DART_SHA256="e391c4ed8f623b9748f897cb585d629057c1141f9eaf8e9b2be118932ba11632"; \
DART_SHA256="7369fcbfb3c96ed85511224580a8ce0e5ecfc8227af563d396ede2055cdf84e1"; \
SDK_ARCH="x64";; \
arm64_stable) \
DART_SHA256="06dd7c6eb6c903f5df8b23f9a35f7b1c35ccb869be6b5019c7dd93868ae2bfbf"; \
DART_SHA256="1c8d96c0b718873f9ae6d9aa7d866cddefdb86f8b1584cbdf589670d65b7c167"; \
SDK_ARCH="arm64";; \
amd64_beta) \
DART_SHA256="e391c4ed8f623b9748f897cb585d629057c1141f9eaf8e9b2be118932ba11632"; \
DART_SHA256="602d5936c3fad0776b999df2090c4c53354891f386fa71e0d7de9923e94c16e6"; \
SDK_ARCH="x64";; \
arm64_beta) \
DART_SHA256="06dd7c6eb6c903f5df8b23f9a35f7b1c35ccb869be6b5019c7dd93868ae2bfbf"; \
DART_SHA256="a1538410aed74e4f1c42962044ec7341b5ea6a74f815b049352f72c1f75e988b"; \
SDK_ARCH="arm64";; \
amd64_dev) \
DART_SHA256="688f8b5238632f53f6cbddee7ded17c665dccb00fb639ccab32a43437f18a4c8"; \
DART_SHA256="bb9bed8d193c5557f617eb4b89d4aee5894a806d5560157fbe6f5142ac1fadd0"; \
SDK_ARCH="x64";; \
arm64_dev) \
DART_SHA256="86c628337919bf7802a8551ce338046a95035dd7f075363811576e2828a0d415"; \
DART_SHA256="08379b473b7b9fd60ebafee20e4646053b0395e3d2eed49b243b93802a1ec201"; \
SDK_ARCH="arm64";; \
esac; \
SDK="dartsdk-linux-${SDK_ARCH}-release.zip"; \
Expand Down Expand Up @@ -80,7 +80,7 @@ CMD ["./tool/test.sh"]
FROM dart as node
RUN set -eu; \
NODE_PPA="node_ppa.sh"; \
NODE_SHA256=27932797347f900242caaaeba5c1d7c965b3da70566d81123b15be1c0b80cc2c; \
NODE_SHA256=99b85ba817cafd99b50e292a6690d8663b27a76630ac6339c2fc186e3333f4a3; \
curl -fsSL https://deb.nodesource.com/setup_lts.x -o "$NODE_PPA"; \
echo "$NODE_SHA256 $NODE_PPA" | sha256sum --check --status --strict - || (\
echo -e "\n\nNODE CHECKSUM FAILED! Run tool/fetch-node-ppa-sum.sh for updated values.\n\n" && \
Expand Down
6 changes: 6 additions & 0 deletions examples/async_await/lib/practice_errors/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// ignore_for_file: always_declare_return_types, type_annotate_public_apis

// #docregion
// Implement changeUsername here
changeUsername() {}
// #enddocregion
11 changes: 11 additions & 0 deletions examples/async_await/lib/practice_errors/solution.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'test.dart';

// #docregion
Future<String> changeUsername() async {
try {
return await fetchNewUsername();
} catch (err) {
return err.toString();
}
}
// #enddocregion
112 changes: 112 additions & 0 deletions examples/async_await/lib/practice_errors/test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import 'package:examples_util/codelabs.dart';

import 'solution.dart';

const _result = result;

// #docregion
List<String> messages = [];
bool logoutSucceeds = false;
const passed = 'PASSED';
const noCatch = 'NO_CATCH';
const typoMessage = 'Test failed! Check for typos in your return value';
const oneSecond = Duration(seconds: 1);

class UserError implements Exception {
String errMsg() => 'New username is invalid';
}

Future<String> fetchNewUsername() {
var str = Future.delayed(oneSecond, () => throw UserError());
return str;
}

void main() async {
try {
messages
..add(makeReadable(
testLabel: '',
testResult: await asyncDidCatchException(changeUsername),
readableErrors: {
typoMessage: typoMessage,
noCatch:
'Did you remember to call fetchNewUsername within a try/catch block?',
}))
..add(makeReadable(
testLabel: '',
testResult: await asyncErrorEquals(changeUsername),
readableErrors: {
typoMessage: typoMessage,
noCatch:
'Did you remember to call fetchNewUsername within a try/catch block?',
}))
..removeWhere((m) => m.contains(passed))
..toList();

if (messages.isEmpty) {
_result(true);
} else {
_result(false, messages);
}
} catch (e) {
_result(false, ['Tried to run solution, but received an exception: $e']);
}
}

////////////////////////////////////////
///////////// Test Helpers /////////////
////////////////////////////////////////
String makeReadable({
required String testResult,
required Map<String, String> readableErrors,
required String testLabel,
}) {
if (readableErrors.containsKey(testResult)) {
var readable = readableErrors[testResult];
return '$testLabel $readable';
} else {
return '$testLabel $testResult';
}
}

void passIfNoMessages(List<String> messages, Map<String, String> readable) {
if (messages.isEmpty) {
_result(true);
} else {
final userMessages = messages
.where((message) => readable.containsKey(message))
.map((message) => readable[message]!)
.toList();
print(messages);

_result(false, userMessages);
}
}

///////////////////////////////////////
//////////// Assertions ///////////////
///////////////////////////////////////
Future<String> asyncErrorEquals(Function fn) async {
var result = await fn();
if (result == UserError().toString()) {
return passed;
} else {
return 'Test failed! Did you stringify and return the caught error?';
}
}

Future<String> asyncDidCatchException(Function fn) async {
var caught = true;
try {
await fn();
} on UserError catch (_) {
caught = false;
}

if (caught == false) {
return noCatch;
} else {
return passed;
}
}
// #enddocregion
17 changes: 17 additions & 0 deletions examples/async_await/lib/practice_using/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// ignore_for_file: always_declare_return_types, type_annotate_public_apis
import 'package:examples_util/codelabs.dart';

// #docregion
// Part 1
// You can call the provided async function fetchRole()
// to return the user role.
Future<String> reportUserRole() async {
TODO('Your implementation goes here.');
}

// Part 2
// Implement reportLogins here
// You can call the provided async function fetchLoginAmount()
// to return the number of times that the user has logged in.
reportLogins() {}
// #enddocregion
13 changes: 13 additions & 0 deletions examples/async_await/lib/practice_using/solution.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'test.dart';

// #docregion
Future<String> reportUserRole() async {
var username = await fetchRole();
return 'User role: $username';
}

Future<String> reportLogins() async {
var logins = await fetchLoginAmount();
return 'Total number of logins: $logins';
}
// #enddocregion
117 changes: 117 additions & 0 deletions examples/async_await/lib/practice_using/test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import 'package:examples_util/codelabs.dart';

import 'solution.dart';

const _result = result;

// #docregion
const role = 'administrator';
const logins = 42;
const passed = 'PASSED';
const testFailedMessage = 'Test failed for the function:';
const typoMessage = 'Test failed! Check for typos in your return value';
const didNotImplement =
'Test failed! Did you forget to implement or return from ';
const oneSecond = Duration(seconds: 1);
List<String> messages = [];
Future<String> fetchRole() => Future.delayed(oneSecond, () => role);
Future<int> fetchLoginAmount() => Future.delayed(oneSecond, () => logins);

void main() async {
try {
messages
..add(makeReadable(
testLabel: 'Part 1',
testResult: await asyncEquals(
expected: 'User role: administrator',
actual: await reportUserRole(),
typoKeyword: role),
readableErrors: {
typoMessage: typoMessage,
'null': '$didNotImplement reportUserRole?',
'User role: Instance of \'Future<String>\'':
'$testFailedMessage reportUserRole. Did you use the await keyword?',
'User role: Instance of \'_Future<String>\'':
'$testFailedMessage reportUserRole. Did you use the await keyword?',
'User role:':
'$testFailedMessage reportUserRole. Did you return a user role?',
'User role: ':
'$testFailedMessage reportUserRole. Did you return a user role?',
'User role: tester':
'$testFailedMessage reportUserRole. Did you invoke fetchRole to fetch the user\'s role?',
}))
..add(makeReadable(
testLabel: 'Part 2',
testResult: await asyncEquals(
expected: 'Total number of logins: 42',
actual: await reportLogins(),
typoKeyword: logins.toString()),
readableErrors: {
typoMessage: typoMessage,
'null': '$didNotImplement reportLogins?',
'Total number of logins: Instance of \'Future<int>\'':
'$testFailedMessage reportLogins. Did you use the await keyword?',
'Total number of logins: Instance of \'_Future<int>\'':
'$testFailedMessage reportLogins. Did you use the await keyword?',
'Total number of logins: ':
'$testFailedMessage reportLogins. Did you return the number of logins?',
'Total number of logins:':
'$testFailedMessage reportLogins. Did you return the number of logins?',
'Total number of logins: 57':
'$testFailedMessage reportLogins. Did you invoke fetchLoginAmount to fetch the number of user logins?',
}))
..removeWhere((m) => m.contains(passed))
..toList();

if (messages.isEmpty) {
_result(true);
} else {
_result(false, messages);
}
} on UnimplementedError {
_result(false, [
'$didNotImplement reportUserRole?',
]);
} catch (e) {
_result(false, ['Tried to run solution, but received an exception: $e']);
}
}

////////////////////////////////////////
///////////// Test Helpers /////////////
////////////////////////////////////////
String makeReadable({
required String testResult,
required Map<String, String> readableErrors,
required String testLabel,
}) {
if (readableErrors.containsKey(testResult)) {
var readable = readableErrors[testResult];
return '$testLabel $readable';
} else {
return '$testLabel $testResult';
}
}

///////////////////////////////////////
//////////// Assertions ///////////////
///////////////////////////////////////
Future<String> asyncEquals({
required String expected,
required dynamic actual,
required String typoKeyword,
}) async {
var strActual = actual is String ? actual : actual.toString();
try {
if (expected == actual) {
return passed;
} else if (strActual.contains(typoKeyword)) {
return typoMessage;
} else {
return strActual;
}
} catch (e) {
return e.toString();
}
}
// #enddocregion
16 changes: 16 additions & 0 deletions examples/async_await/lib/putting_together/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// ignore_for_file: always_declare_return_types, type_annotate_public_apis

// #docregion
// Part 1
addHello(String user) {}

// Part 2
// You can call the provided async function fetchUsername()
// to return the username.
greetUser() {}

// Part 3
// You can call the provided async function logoutUser()
// to log out the user.
sayGoodbye() {}
// #enddocregion
Loading