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

first baby steps towards native browser support for the JS sdk of testground #25

Closed
wants to merge 5 commits into from
Closed
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: 5 additions & 1 deletion src/runtime/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ const { parseRunParams } = require('./params')
* @returns {RunEnv}
*/
function currentRunEnv () {
return parseRunEnv(process.env)
if (process.title === 'browser') {
return parseRunEnv(window.testground.env)
} else {
return parseRunEnv(process.env)
}
Copy link
Contributor

@laurentsenta laurentsenta Nov 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this standard? Else, could we simplify with something like process.env || window.testground.env?

Got it, that's in the PR description.
Could we move the checks like if proces.title into a single function if (isBrowser()) for example?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will drop this in favour of the browser spec.

}

/**
Expand Down
12 changes: 9 additions & 3 deletions src/runtime/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,20 @@ function getLogger (params) {

const transports = [
new winston.transports.Console({ format }),
new winston.transports.File({ filename: 'stdout', format })
]

if (params.testOutputsPath) {
if (process.title !== 'browser') {
transports.push(new winston.transports.File({
format,
filename: path.join(params.testOutputsPath, 'run.out')
filename: 'stdout'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is due to a merge from an older master version. Should I remove this all together?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand, what did you merge? What would you like to remove?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure anymore, going to clean it up.

}))

if (!params.testOutputsPath) {
transports.push(new winston.transports.File({
format,
filename: path.join(params.testOutputsPath, 'run.out')
}))
}
}

return winston.createLogger({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(creating a thread)

You listed a lot of fallback libraries, do you know what purpose they solve? (for example the path library might "just" be required by the winston logger).

@achingbrain shared an interesting option: It might be enough to use the browser field to shim the features that are not isomorphic (the loggers creation and the env retrieval): https://github.com/defunctzombie/package-browser-field-spec#replace-specific-files---advanced

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They solve the purpose of being able to run the testground plan without runtime errors, so that collection get assembled in a trial-error approach. I'll look into the browser field, haven't used that before, so if you could give some practical example of how you see that that would be great. Either way I'll give it a look.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok got'cha, you shared it in the other thread already :)
(#25 (comment))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to get rid of the fallback libraries however we might need to either make winston-js browser friendly (e.g. see winstonjs/winston#2196) or from our end (sdk-js) make the logging different for browser vs node.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This winstonjs/winston#287 was opened in 2013

It's very likely this will take us month(s) to get browser support in winston,
I'd recommend you try this: https://stackoverflow.com/a/55433049/843194
and else, "just" return a console object when we're in the browser.

We can tackle full support for logging as a follow-up issue (related: testground/testground#1355)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not to worry, that’s already an approach I’m taking on my local replace branch. Sorry if i didn’t communicate that yet

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(creating a thread)

You listed a lot of fallback libraries, do you know what purpose they solve? (for example the path library might "just" be required by the winston logger).

@achingbrain shared an interesting option: It might be enough to use the browser field to shim the features that are not isomorphic (the loggers creation and the env retrieval): https://github.com/defunctzombie/package-browser-field-spec#replace-specific-files---advanced

A few examples he shared:

// get-env.js
export function getEnv() {
  return process.env
}

// get-env.browser.js
export function getEnv {
  return window.testground.env
}

// package.json
"exports": { // used by external imports
  "./get-env": {
    "browser": "get-env.browser.js",
    "import": "get-env.js"
  }
},
"browser": { // used by internal imports (e.g by esbuild before building code for running unit tests)
  "get-env.js": "get-env.browser.js"
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh ok nice, so kinda like what you would do with C macro's in C/C++, go file suffixes and cfg-like macro's in Rust. Got-cha. That does look a lot cleaner indeed. Will play with it this week and see how far I get.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok read the spec, seems fine. Going to adapt the PR to this format.

Expand Down